Solved

Objects as a data member of another class

Posted on 2003-12-08
6
240 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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