-1

I have a class which is derived from a data container class. The constructor is not able to initialize all of the contents of the container, so it is necessary to call another class member function (providing some additional data) to finish off initialization. This member function sets a boolean to true, stating that data is ready for access.

If the data was a member of the class, I'd set it private/protected and I'd have an accessor such as:

mycontainer& get_data() { check_macro(ready, "Data not ready"); return data; }

How do I do this if the class is inherited from mycontainer ? Can I derive it from mycontainer privately and then expect that returning a reference to this will be public?

Joce
  • 2,220
  • 15
  • 26
  • Can you refactor the constructor so it does not exit until the object is fully initialized? – NathanOliver Aug 03 '17 at 14:28
  • First something is fishy with your non constructor initialization, second your final sentence is pretty nonsensical. You are going to have to provide a better explanation than you did if you want any help with this. – Krupip Aug 03 '17 at 14:34
  • @NathanOliver: This is do-able in my present case, and I agree it is better style. However, I think there are cases when it is not logical to fully initialize the object when it is declared, e.g. part of the data stored may be calculated by a non-const member which is unknown at the time of declaration. – Joce Aug 04 '17 at 07:12

1 Answers1

1

Keep in mind that when you inherit from another class, your new class has all of the members that the base class has. You just access them directly as members of your class.

In the example below, the derived class has no members, but since it inherits from 'DataContainerClass', it has all of the members of that class, thus it has the 'Ints' member.

So, whatever members your data container base class has, you just access as though they were defined in your class. The members must be accessible, of course. (public in the base class, for example)

class DataContainerClass
{
    public std::vector<int> Ints;
}



class MyClass : public DataContainerClass
{
}

int main()
{
    MyClass cls;

    // Ints is visible as a member of 'MyClass', because
    // MyClass inherits from DataContainerClass.
    cls.Ints.push_back(1);
    cls.Ints.push_back(2);

}
ttemple
  • 852
  • 5
  • 20
  • Thanks. In your example, if I change `MyClass` to `class MyClass : private DataContainerClass`, then I need to add an accessor to return `Ints` in order to use it in `main`. And my question is, if I have `class MyClass : private std::vector`, can I define an accessor to the base class `vector` ? – Joce Aug 04 '17 at 07:17
  • In that case, you don't need an accessor. The use would be – ttemple Aug 12 '17 at 12:34
  • If you make the inheritance public, you wouldn't need an accessor. With private inheritance, you will. See this for a good discussion of what you are trying to do: https://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector – ttemple Aug 12 '17 at 12:41