Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Objects as a data member of another class

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
garge
Asked:
garge
  • 4
1 Solution
 
AlexFMCommented:
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
 
AlexFMCommented:
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
 
monkesdbCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gargeAuthor Commented:
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
 
AlexFMCommented:
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
 
AlexFMCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now