Solved

Inheritance design question

Posted on 2006-11-16
5
218 Views
Last Modified: 2010-04-01
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
Comment
Question by:ambuli
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 17959334
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
 
LVL 4

Assisted Solution

by:Raymun
Raymun earned 100 total points
ID: 17959694
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 17961104
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
 
LVL 6

Assisted Solution

by:SJT2003A
SJT2003A earned 150 total points
ID: 17966614
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
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 17967684
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question