Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Inheriting from structs?  (Read 1484 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Inheriting from structs?
« on: August 25, 2013, 07:59:21 pm »
Hello i just got some problems and I got confused.

I have POD that handles typical attributes for a game entity.So I presented it this way:

#include<iostream>
#include<string>
using namespace std;

struct Attribute
{
    string directory;
    string name;
};

struct TileAttribute : public Attribute
{
    int cost;
};

struct AssetAttribute : public Attribute
{
    int cost;
};

int main()
{
    Attribute * attribute = new TileAttribute;
    attribute->cost; // no such thing as cost in TileAttribute; error
// WHAT's the big deal if I use "."? I am asking why can't I access cost? I think I am missing something here
// Thanks for the correction anyway.
    return 0;
}

 

I recreated this snippet using this code and I can't figure out how am I suppose to inherit just variables on deriving structs? Is this even possible? I have a container for attribute and I don't want creating a separate containers for two.  Well I could save myself some troubles if I could write this as a full pledge class but I just want handler that handle values distinct to tiles and asset so I just used POD.

How can I fix this?

Thanks for your help!
« Last Edit: August 26, 2013, 04:44:58 am by magneonx »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Inheriting from structs?
« Reply #1 on: August 25, 2013, 08:11:48 pm »
I have POD that handles typical attributes for a game entity.So I presented it this way:
With std::string, it's not a POD (plain old data) type.

Your problem is that you access a pointer in a wrong way (. instead of ->), you should probably read more about C++. And don't use raw pointers that own their memory, work with std::unique_ptr<Attribute> instead of Attribute*. By the way, the keywords struct and class are equivalent except for the default visibility with respect to members and inheritance.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Inheriting from structs?
« Reply #2 on: August 26, 2013, 04:42:13 am »
Its I a typo Nexus. I have fixed it. The question is not about the syntax anyway. I was tired and left off that morning. And no I didn't pasted it so I just typed it on.

And yes I know about C++ and I shouldn't be using SFML C++ in the first place eh?
And no, I don't use unique_ptr I use shared_ptr in my case. I store those attributes in a map< string , shared_ptr< Attribute > >.

I have to store it on shared_ptr< Attribute > that's all.

AND I will reiterate my question. Actually I have two attributes one for tiles one for assets. I don't have to post my entire code here.

If I say shared_ptr< Attribute > p( new TileAttribute );
p.get()->cost;

I could not get p.get()->cost. It throws error regarding that Attribute struct has no such member. It seems I can't inherit data from deriving struct? I can write a class that just have accessors on them but honestly this is just data that I wanted it to hold. No functions, no constructors or anything. Just organizing data. But how am I suppose to do this? Is this even possible in C++? I've read about structs a long time ago and I can say I haven't encountered inheriting data from a deriving struct. I need to organize my data for my Resource class.

What I expect is that:
shared_ptr< Attribute > p( new TileAttribute );
I could get p.get()->cost; well I could do this via writing a class with accessors and constructors but I have said it is too verbose and I just want data on single object only.

How can we do this?

The syntax is not inquestion it is the inheritance of data on struct on question. And I am so sorry if I got anyone confused I edited my posted code now
« Last Edit: August 26, 2013, 04:53:03 am by magneonx »

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Inheriting from structs?
« Reply #3 on: August 26, 2013, 04:48:26 am »
Quote
With std::string, it's not a POD (plain old data) type.

I see, I think I just write classes then. I just wanted to prevent myself writing to much for simple object that I intended to use to organize sets of data that describes a particular entity. If that is the case I am better off with classes with accessor methods.

I don't even wanted to try char *. Because I could not guarantee the length the name of entities on my Resource directory.

So thanks for that also Nexus!
« Last Edit: August 26, 2013, 04:54:46 am by magneonx »

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Inheriting from structs?
« Reply #4 on: August 26, 2013, 05:50:52 am »
I finally got what I wanted using this code. I used dynamic_cast to cast the pointer back to the derive class. I don't know if this is such a good idea/practice but at least I get to store it on my
map< string , shared_ptr< Attribute > > AttributeLibrary
without making a separate containers for tile and asset attributes.

So here's what I have done so far:
#include<iostream>
#include<string>
#include<map>
#include<tr1/memory>
using namespace std;

class Attribute;

typedef map< string , shared_ptr< Attribute > > Library;

class Attribute
{
    public:

        Attribute()
        {

        }

        virtual ~Attribute()
        {

        }

        virtual void setDirectory( const string &d )
        {
            directory = d;
        }

        virtual void setName( const string &n )
        {
            name = n;
        }

        virtual const string & getName() const
        {
            return name;
        }

        virtual const string & getDirectory() const
        {
            return directory;
        }

    private:

        string directory;
        string name;

};

class TileAttribute : public Attribute
{
    public:

        TileAttribute()
        {

        }
        virtual ~TileAttribute()
        {

        }

        void setCost( int c )
        {
            cost = c;
        }

        int getCost() const
        {
            cout << " --from TileAttribute ";
            return cost;
        }

    private:
        int cost;

};

class ActorAttribute : public Attribute
{
    public:

        ActorAttribute()
        {

        }
        virtual ~ActorAttribute()
        {

        }

        void setCost( int c )
        {
            cost = c;
        }

        int getCost() const
        {
            cout << " --from ActorAttribute ";
            return cost;
        }

    private:
        int cost;

};

int main()
{
    shared_ptr< Attribute > p( new TileAttribute );
    shared_ptr< Attribute > p1( new ActorAttribute );

    TileAttribute * pT = dynamic_cast< TileAttribute* >( p.get() );
    ActorAttribute * pT1 = dynamic_cast< ActorAttribute* >( p1.get() );

    pT->setCost( 100 );
    pT1->setCost( 200 );

    cout << "The cost of the tile is  : " << pT->getCost() << endl;
    cout << "The cost of the actor is : " << pT1->getCost() << endl;


    return 0;
}

 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Inheriting from structs?
« Reply #5 on: August 27, 2013, 11:30:50 am »
You misunderstood. POD has nothing to do with struct or class. These two keywords are absolutely equivalent, except for the default visibility (as stated in my last post). Please lookup the definition of Plain Old Data for more information.

Inheritance with structs works perfectly fine, but of course you have to downcast a base class pointer if you want to access members of the derived class. Note that dynamic_cast is mostly a design error, as it violates the principle of dynamic polymorphism. You should try to provide an interface using virtual functions. If you know the dynamic type for sure, use static_cast to downcast.

You should replace std::shared_ptr with std::unique_ptr, because you don't use shared ownership. Like this you can avoid the large overhead of shared pointers.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything