Solved

Objects as a data member of another class

Posted on 2003-12-08
6
238 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:garge
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

12 Experts available now in Live!

Get 1:1 Help Now