• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Inheritance design question

Hi Experts,

I have the following problem.

#include <iostream>
#include <string>
using namespace std;

class M{};
class N{};

class A{
};

class B: public A
{
private:
     M m_memM;
};

class C: public A
{
private:
   N m_memN;

};
int main()
{
   return 0;
}
In the code snippet above, class B, C are derived from class A.
For B, it should have a member of class M
For class C, it should have a member of class N.  That is, the derived classes of A differ by their member.  class M, class N are not related.

The way, I defined above seem not to work. The reason being parent class not being able to access membes of derived class.  It was pointed out in one of my earlier question and after some testing I found that is what happening.

So, I had to move the members as below.
class A{
protected:
   M m_memM;
   N m_memN;
};

class B: public A
{
private:

};

class C: public A
{
private:

};


But, I smell that having member variables that will be used by some derivation of the parent class is not a good design.  Is it so?  How can I prevent this?  
(The problem is related to my previous question on void pointers).  
0
ambuli
Asked:
ambuli
4 Solutions
 
jkrCommented:
Why should all the dreived classes inherit members that they do not need? Something that is not common to all derived classes should not be put in a base class for good reasons.
0
 
RaymunCommented:
Hi,

A member of a derived class is private to that class, which is why, in the first example, the parent class cannot access it.

Both are good designs, depending on the application. If a derived class does not need to have both M and N objects, then the first example would be better than the second.

In the second example, the protected members, of type M and N, are inherited by any class that derives from class A. This means that both classes B and C have copies of m_memM and m_memN from parent A. Note that these, and any other, inherited members can be overridden as well. This example would be good in situations where all derived classes must have m_memM and m_memN as members.

ray


0
 
DanRollinsCommented:
Since you are using void pointers (from your previous Q http:/Q_22062544.html) then why not just coerce the pointer a  B* or a C* (rather than an A*)??  That should let you access the desired members.
0
 
SJT2003ACommented:
I would go for the first type of definition/declaration. As a base class defines the common characteristics of all its derived classes and mostly all those characteristics are used by its derived classes, I would not declare a member where it may be used in one and not in all others. I would not say it is a good design.
Hence I would define the derived classes with those which are specific to that class and not common in other siblings.

And for your question, why you want to use base class to access the members of its derived???
A base class is parent and defines only those are common of all its derived while the derived has its own class-specific characteristics. The class-specific behaviour is only accessed by the derived and that is what the derived definitiion is for.
Accessing the derived class-specific features thru its base class is not allowed nor a right idea.

Could you please elaborate the scenario where you would like to access it such way???

Good luck :)
0
 
DanRollinsCommented:
SJT2003A,
As I understand the situation, ambuli's reasons relate to the need to pass an object to a function as a void pointer
     http:/Cplusplus/Q_22062544.html
It's not that unusual of request.
In MFC, a common scenario is that all you have is a CWnd* to pass, but you know that it is a pointer to a particular derived object, such as, say, a CListView or a CTreeCtrl.  If you know which kind of object it is, you can coerce the pointer into the correct type and then access the correct (specialized) properties and member functions.  If you don't, you can still use access the common base-class members.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now