Inheritance design question

Posted on 2006-11-16
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
     M m_memM;

class C: public A
   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{
   M m_memM;
   N m_memN;

class B: public A


class C: public A


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).  
Question by:ambuli
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Assisted Solution

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.

Assisted Solution

Raymun earned 100 total points
ID: 17959694

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.


LVL 49

Expert Comment

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.

Assisted Solution

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 :)
LVL 49

Accepted Solution

DanRollins earned 200 total points
ID: 17967684
As I understand the situation, ambuli's reasons relate to the need to pass an object to a function as a void pointer
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

717 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