Solved

Objects as a data member of another class

Posted on 2003-12-08
6
244 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

623 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