tags:

views:

1051

answers:

4

When do you use each inheritance?

class Base{};

class Derived: protected Base{};
class Derived2: public Base{};

My case:

I have class called Snapshot which only contains GetXXX methods. It is a light-weight classed used to store current state of the Value class. I also use it for recovery, keeping instances of this class long after Value class instance are gone. A Manager class, processes instances of the Snapshot class. Let me show you the code:

class Snapshot
{
        public:
            Snapshot (const Snapshot * snap)
            {
              _x=snap->_x;   
              _y=snap->_y;  
              _z=snap->_z;  
            }
            Snapshot (){_x=_y=_z=0;}
            int GetX(){return _x;}
            int GetY(){return _y;}
            int GetZ(){return _z;}
            ~virtual Snapshot(){} 

         protected: 
             int _x,_y,_z;               
};

class Value:public Snapshot 
{

 /*Very heavy class with a lot of components used to calculate _x, _y, _z*/


};


class Manager
{
       public:
         void Process( const Snapshot * snap)
         {

         }  
};

How do you feel about this design? What are the alternatives?

Thanks

Solutions and issues

  • Solution: I would create makeSnapshot function which would return Snapshot object by given Value object.

  • Issues:

    • major issue: I sent snapshots at very frequently (every second, even less), hence I don't want to incur the construction and destruction cost minor issue:

    • semi-major issue I will have to make Value a friend of Snapshot, as I don't want
      to introduce setters.

+2  A: 

As for the question about the private and protected inheritance, the pretty thorough explanation can be found here:

Main issue is the semantic - whether something IS-A something, or whether something is IS-IMPLEMENTED-IN-TERMS-OF something.

Anonymous
this is one is both: it is something and it uses something :)
+2  A: 

I would create makeSnapshot function which would return Snapshot object by given Value object.

Do you really need Value to be a Snapshot?

Also you can consider GOF design pattern "Memento".

Mykola Golubyev
I though about it, and it introduces two issues:major issue: I sent snapshots at very frequently (every second, even less), hence I don't want to incur the construction and destruction costminor issue: I will have to make Value a friend of snapshot, as I dont want to introduce setters. thx
So where Memento doesn't meat your requirements?
Mykola Golubyev
read it again...
A: 

Generally speaking I would use public inheritance, if I want to implement a specific interface, e.g. if my class is to be accessed thrugh a specific contract. Protected inheritance could be used, if you just want to reuse the functionality implemented in the parent.

I would make Snapshot a pure virtual class, e.g. just an interface, and Value would implement the getXYZ methods. E.g. you probably don't need the _x,_y,_z members in Snapshot.

siddhadev
in recovery I use instances of Snapshot class...
A: 

Colour me Luddite, but in over 20 years of C++ programming I have never used protected or private inheritance, and have never seen an example of their use which could not have been done better using some other language feature (usually containment).

anon
I've seen private on numerous occasions, but protected reeks of being somewhat pathological ;)
Anonymous
Same here, never seen protected inheritance, though my experience falls short of 20 years :))
It's rare, but I can think of a few reasons for private inheritance. Protected, though, I have never seen.
rlbond
I found a use for protected inheritance only once so far in my career. It involved propogating an interface to derived classes while hiding it from public users. Private inheritance is much more common.
Brian Neal