3

Let say I have some data structure in a class as follow

class DataStructure
{
    DataStructure();
    ~DataStructure();

    void reset();
    void init();
    void setiX();
    ...;
    int getiX();
    double get dx();
    void addToList(OtherDataStructure dt);

private:
    int ix;
    int iy;
    int iz;
    double dx;
    ...;
    vector<OtherDataStructure> dtStVec;
};

so I usually have this class used as the following way

class manageSomething
{
    manageSomething();
    ~manageSomething();

    func1();
    func2();
    ...;
    funcN();
private:
    some vatiables;
    DataStructure structure; //HERE
};

so I usually have to use getters and setters to access the data structure variables

is it better inherit the data structure, and access all element directly, if the inheriting class is not in the main application, as follow

class manageSomething : public DataStructure
{
    manageSomething();
    ~manageSomething();

    func1();
    func2();
    ...;
    funcN();
private:
    some vatiables;
};

so the usage of manageSomething is used as

int main()
{
   manageSomething manager;
   ///manager.stuff ....
   return EXIT_SUCCESS;
}

and when do we decide which one to choose from?

aah134
  • 860
  • 12
  • 25

3 Answers3

1

You should not inherit publicly unless the class that you are writing indeed extends the class from which you are inheriting, i.e. manageSomething is a kind of DataStructure. This is known as Liskov substitution principle. Violating it leads to serious readability and maintainability issues in your code.

The rules are less strict with private inheritance, but in general you should prefer composition to private inheritance, except under very specific circumstances.

The upshot of this is that your first code snippet (with the composition) does everything right, you do not need to change it.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • let say we inherit it privately, because I dont what any function to be seen in main app outside the class – aah134 Jun 27 '13 at 14:43
  • @abdul Private inheritance is legitimate, but it's a maintenance liability: someone changing something in `DataStructure` can break your `manageSomething` much more easily. [See this short article from C++ faq](http://www.parashift.com/c%2B%2B-faq-lite/priv-inherit-vs-compos.html) for details. – Sergey Kalinichenko Jun 27 '13 at 14:47
  • the example of class Wilma, and class Fred : private Wilma, is very nice :) nice way to call a function within inheriting classes from parent – aah134 Jun 27 '13 at 16:14
1

That's really depend on the case !

If you have more than 1 DataStructure in your manager, you need to have a manager with a STL containers of DataStructure.

If you have only ONE DataStructure in your manager, your manager is probably useless et you can use directly a DataStructure. BUT if you think that it is needed cause you call function which haven't their in DataStructure you can inherit of DataStructure.

I need to have more precision of what you do with you DataStructure for a better answer please.

Shar
  • 455
  • 2
  • 14
1

First,

class manageSomething 
{
manageSomething();
~manageSomething();

func1();
func2();
...;
funcN(); 
private:
some vatiables;
DataStructure structure; //HERE 
};

This is part of your code, I want to tell you, that all your class is private. C++ makes everything private as a default.

Going back to your question. In most cases best way is getters and setters. Never use public varables. There is difficult to tell if inheritance is the best option, sometimes it is, but not often. There is one good but not perfect rule for inheritance. You need to determine if object has some other object or is that object.

class Engine
{
   ...
};

class Car
{
private:
    Engine m_engine;
...
};
class BMW : protected Car
{
...
};

In this example BMW is car, so it is better to use inheritance, however car has engine, so we put this object inside this class. What is more about inheritance, it is better usually inherit in protected way. You can use public inheritance if you want to inherit that class. Let's say we use public inheritance for this:

class BMW : public Car
{
...
};
class X7 : protected Car
{
...
};

I hope it helped a little.

ST3
  • 8,826
  • 3
  • 68
  • 92