Solved

assigning a child class its parent

Posted on 2014-02-25
4
566 Views
Last Modified: 2014-03-12
Hi

I have a class hierarchy as follows:
A (parent)->B(child)


I create an object A. A a=new A();

Somewhere, later in the code, I want to create an object B, called b, and want to specifically assign the object a as its parent. Is there a way to do this in C++?

Normally, I would do something like this:
B b=new B();

B's constructor would make a call to A's constructor
B::B()
:A(){
}

But if i have a parent object already instantiated, is there a way to assign that particular object as a child object's parent at the time the child is instantiated?

thanks
0
Comment
Question by:LuckyLucks
[X]
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
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39887118
There seems to be a misconception. A statement like

B::B()
:A(){
}

Open in new window


only a base class' constructor. That is not related to storing a reference to a specific instance to the base class. If you want to create a relation between two instances of A and B, pass an instance of A to one of B's constructors, e.g.

class B {

public:

  // default constructor
  B() : parent(A()) {
  }
  // constructor that implicitly takes an instance of 'A'
  B(A& a) : parent(a) {
  }

private:

  A& parent;
};

Open in new window


If you need to have to option to instantiate B without a parent, you have to use pointers, since references can't be 'NULL', e.g. like

class B {

public:

  // default constructor
  B() : parent(NULL) {
  }
  // constructor that implicitly takes an instance of 'A'
  B(A* a) : parent(a) {
  }

private:

  A* parent;
};

Open in new window

0
 

Author Comment

by:LuckyLucks
ID: 39888871
In the solution you gave, A and B do not have a base class-derived class relationship. B just keeps a member variable of A.


 B cannot use a function defined in A as would be possible using rules of inheritance (searching the parent class when child class does not have that function).

I am looking for a way where B can be a derived class of A , but as A is already instantiated in earlier code, if i can get an association with that particular instance of A.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 39889215
Sorry, but that's easy to remedy, i.e.

class B : public A {

public:

  // default constructor
  B() : A(), parent(A()) {
  }
  // constructor that implicitly takes an instance of 'A'
  B(A& a) : A(), parent(a) {
  }

private:

  A& parent;
};
                                            

Open in new window


or

class B : public A {

public:

  // default constructor
  B() : A(), parent(NULL) {
  }
  // constructor that implicitly takes an instance of 'A'
  B(A* a) : A(), parent(a) {
  }

private:

  A* parent;
};
                                            

Open in new window


respectively.
0
 
LVL 34

Accepted Solution

by:
sarabande earned 250 total points
ID: 39889784
a base-derived relation is not the same as a parent-child relation. it is actually a totally different relation which could not be matched.

the problem is that if B is derived from A, the B object "IS A" A object as well cause a part of the B object is made up by the A. So, if you have two B objects both have a different A object and never the same. The only exception to that is if you derive B and C from A and derive D from both B and C public virtual, the A base was shared by both the B and C (diamond inheritage). but even that is not a parent relation.

a parent relation means that a 'master' object contains  two or more objects of a 'detail' class. it always means that it were two different classes which were not derived. the parent relation could be defined by using an array or container where the child objects were stored. By deriving the child classes from same base class you even could hold a container of baseclass pointers of the children for virtual use such that the parent could hold different classes. another approach is to have a reference to the parent in the child classes (c++ reference or pointer reference) as shown by jkr. note, you would not derive from parent as it makes no sense to have the parent as part of the class itself.

note, you could make the parent class friend of the child class(es) to have access to their private or protected members. but that is a bad design as it is a violation of the encapsulating principle. each class should care for its own data and you neither should directly access members of the parent by the child nor vice versa.

Sara
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

690 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