Struct Reference Initialisation

Hey there,
I have a custom struct hierarchy that goes vaguely like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Base
{};

struct Derived1 : public Base
{
   int num;
};

struct Derived2 : public Base
{
   char c;
};

// etc. 

I'm using "Base" simply as an umbrella struct, so I can access any of the derived structs with a "base" reference(&).

The issue I'm having is, I have a class that has a data member that is a reference to the struct "Base" but, I'm getting an error that says my constructor for this class doesn't provide an initialiser for that data member.

I've tried intialising a derived object for the reference, like so:

1
2
3
4
5
6
7
MyClass:MyClass()
{
   Derived1 d1;
   d1.num = 0;

   mBaseRef = d1;
}

But, it doesn't work...

Could you give me a better suggestion to my struct hierarchy or tell me what is wrong with my initialisation of my reference member?

Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct base { /* ... */ };

struct my_class
{
    // http://en.cppreference.com/w/cpp/language/initializer_list
    explicit my_class( base& b ) : bref(b) /* initialise bref */ {}

    base& bref ;
};

int main()
{
     struct derived : base { /* ... */ };
     derived d ;
     my_class mc(d) ;
}
Topic archived. No new replies allowed.