Solved

Objects as a data member of another class

Posted on 2003-12-08
6
239 Views
Last Modified: 2010-04-02
Hi,

In my design, I have a class A which contains an object of type B. Eg:

class A
{
private:
    B myBObject;

public:
    A();
    hello();
}

class B
{
private:
    int val;

public:
    B();
    someMethod();
}

In class A, by declaring an object of B eg:
B myObject;
does this mean that I already have an object of type B created in the stack?
Can I then do this:

A someAObject;
someAObject.myBObject.val = 5;

without having to create the B object?

OR:

In a method in class A can I just refer to the B object even though I haven't yet created an A object?
For example, in A's method hello() can I do this?

void A::hello()
{
    myBObject.val = 10;
}

even though nowhere have I actually created an A object or a B object except for the declaration of B in the class A?
0
Comment
Question by:garge
  • 4
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9895457
1) Does this mean that I already have an object of type B created in the stack?
Object B is created in the same place where container object A is created. If A instance is on the stack, internal B object is on the stack.

2)
A someAObject;
someAObject.myBObject.val = 5;

This will not compile because myBObject is private. But if myBObject is public, it's OK.

3)
void A::hello()
{
   myBObject.val = 10;
}

It's OK.

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9895463
myBObject is placed inside of A object. When A object is created, myBObject is created also. When A object is destroyed, myBObject is destroyed.
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9895479
if you only want a one way reference (only A contains a B and not the other way round). all you have to do is define the class B before you define the class A and you can do pretty much everything you expect.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:garge
ID: 9895527
1.  So when I create an A object, the B object also gets created automatically for me, that is the B constructor is automatically called?

2.  However, if instead, I don't have an object of B declared as:
B myBObject;

but as:
B *myBObject;

i.e a pointer to a B object, then somewhere, I would need to manually create the B object myself? eg:

A::hello()
{
    myBObject = new B();
}

3.  This may seem like a silly question, but if I declare a pointer to B, do I have to create an object of B in the heap using "new"?
Can I have something like this?

class A
{
    B *pointerBObj;
    hello();
}

void A::hello()
{
    B anObj;
    pointerBObj = anObj;
}


0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9895642
1) Yes

2, 3) If you keep a pointer to B as class A member, the best way is to create B object in A constructor and delete in destructor:

// A.h
class B;

class A
{
    public:
    A();
    ~A();
...
    B *pointerBObj;
};

// A.cpp
#include "A.h"
#include "B.h"


A::A()
{
    pointerBObj = new B();
}

A::~A()
{
    delete pointerBObj;
}

void A::AnyOtherFunction()
{
    // use pointer to B
    pointerBObj->AnyFunction();
}

Notice that B.h in this case is not included to A.h, only to A.cpp - this allows to remove circular include dependencies.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
ID: 9895676
If you want to create temporary instance of B object in some specific function, you don't need B class member in A.

void A::hello()
{
   B b;
   // use b  
}

or

void A::hello()
{
   B* pb = new B();
   // use pb  
   delete bp;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

914 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now