Struct Inheritance

Ok so I've googled the hell out of my computer on this....

It's a simple problem, I have 2 structs, one is a BaseStruct and the other a ChildStruct. BaseStruct has an Enum called StructType.

I'm trying to set ChildStruct's inherited StructType to a specific value, in itself. Now I come from a C# background so for me this is something like:

1
2
3
4
5
6
7
8
9
struct BaseStruct
{
    StructType sType;
}

struct ChildStruct : BaseStruct
{
    sType = FirstType;  // Or an integer of some sort
}


But obviously this would be more like the following in C++:
1
2
3
4
5
6
7
8
9
struct BaseStruct
{
    Namespace::StructType sType;
};

struct ChildStruct : BaseStruct
{
    ???
};


How can I accomplish this in C++???
You need the constructor for that. Look here:

http://stackoverflow.com/questions/225089/why-cant-we-initialize-members-inside-a-structure

Then structs are inherited from C and although there's nothing wrong with using them in C++, some programmers (and companies as well) are allergic to the use of a struct for anything beyond packing some data together. You may therefore consider class instead.
May I ask, why do you need to do this in the first place? What's the point of using an inheritance model if you're going to use an enum for all the possible derivations?
Because they are the components of an ECS model. So I'm using structures to store the data, I saw no reason to use classes.
The class keyword was precisely introduced because of the OOP nature of C++. It therefore makes more sense to prefer it over struct. You can eventually derive a class from a struct in case you have legacy C code that sets you off on the wrong foot. MFC, if you've heard of this was done in that way.
In C++ there is no difference between class and struct other than the default privacy settings. Any further distinction you make is a personal preference or style convention. For example, I personally never use the class keyword. Just to go against the norm.
Last edited on
"Just to go against the norm."

A man's gotta do what a man's gotta do :D

1
2
3
4
5
6
7
8
9
10
11
12
struct base
{
    enum type { BASE, A, B, C, D };
    type struct_type = BASE ;
    // ...
};

template < base::type TYPE > struct type : base { type() { struct_type = TYPE ; } };

struct derived_A : type<base::A> { /* ... */ };
struct derived_B : type<base::B> { /* ... */ };
struct derived_C : type<base::C> { /* ... */ };

http://coliru.stacked-crooked.com/a/ebac7c124560e6e7
Thank you for the replies.

> YokoTsuno: I'm well aware of this, however, OOP is a completely different design pattern than ECS, so it's nature is kind of irrelevant in this instance. For a component or an entity in an ECS model, most of it's nature is public, and typically ignores encapsulation. Of course you can use encapsulation but ultimately it would be redundant, you'd be making both a Getter and a Setter for your variables that are being accessed from virtually everywhere in your program, therefore it's more logical to use a Structure over a Class, and only set the things you need protected or private by using said keywords.

> JLBorges: Ah.... I created my enum in a separate header outside of my base class but within the same namespace, since the enum is used by multiple different parts, both the Components and the Systems, Components being a group of structures and Systems being a group of classes. But thank you for the info, I'll bare that in mind.
Topic archived. No new replies allowed.