?
Solved

How to instantiate class within a class?

Posted on 2013-11-26
7
Medium Priority
?
522 Views
Last Modified: 2013-11-27
Say I have a class ClassMain, and ClassMain needs to have a pointer to a ClassA. If I do the following:
ClassMain.h
#pragma once
#include "ClassA.h"

class ClassMain
{
public:
	ClassMain(void);
	ClassA *aPtr;
};

Open in new window

ClassMain.cpp
#include "StdAfx.h"
#include "ClassMain.h"

ClassMain::ClassMain(void)
{
	ClassA a;
	aPtr = &a;
}

Open in new window

My question is about creating the instance a of ClassA in the constructor of ClassMain. It looks like a will go out of scope the moment the constructor finishes executing, leaving my pointer aPtr pointing to an object that is all ready for garbage collection.

Is that a problem?

And while I'm at it I might as well ask how this would be done using an initialization list. Or is there an even better way of doing this?

(This will be an unmanaged code project. I like defining ClassA a at compile time rather than using a new statement.)
0
Comment
Question by:deleyd
[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
7 Comments
 
LVL 25

Assisted Solution

by:chaau
chaau earned 1000 total points
ID: 39679849
You have two options. One you have mentioned yourself: use an operator new() to create a new class. You will need to delete the pointer in the ClassMain destructor. This method is useful when there is no default constructor for the ClassA.
Imagine this situation:
class ClassA:
{
public:
  ClassA(int a) {_a = a;}
private:
  int _a;
};
For the class ClassA there is no default constructor. The only way to create the ClassA variable is to have an int parameter passed in.
In this case you instantiate the variable in your ClassMain constructor like this:
ClassMain::ClassMain(void)
{
	aPtr = new ClassA(5);
}

Open in new window

However, in your case, you can simply create an instance variable for the ClassA inside your ClassMain (this is by the way the second option), like this:
class ClassMain
{
public:
	ClassMain(void);
	ClassA a;
};

Open in new window

In this case ClassA will exist together with ClassMain, and will go in/out of scope with the ClassMain objects
0
 

Author Comment

by:deleyd
ID: 39679885
Does
ClassA a;

Open in new window

go in the ClassMain.h file?

I thought of putting it there, but then wondered if that meant every one who did #include ClassMain.h would be instantiating an instance of ClassA?
0
 
LVL 25

Accepted Solution

by:
chaau earned 1000 total points
ID: 39679892
If you put ClassA a; inside ClassMain, like this:
class ClassMain
{
public:
	ClassMain(void);
	ClassA a;
};

Open in new window

Then they would not. They will only instantiate the instance of ClassA only when they creat an instance of ClassMain
0
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.

 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 500 total points
ID: 39680878
You mentioned that ClassMain needs "a pointer".  Any particular reason for this?  If not, then what chaau has shown above is just fine... you're simply creating an instance of ClassA within ClassMain.

But by saying you "need a pointer" implies the ClassA object is coming from outside of ClassMain.  In that case, what you could do is have a pointer to a ClassA object defined in ClassMain, and initialize it via ClassMain constructors and member functions.

//ClassMain.h
class ClassA;    //Forward declaration... says the class will exist without giving details
class ClassMain
{
  private:
    ClassA* m_pA;  //Forward declaration allows us to define a pointer variable without details

  public:
    ClassMain();
    ClassMain( ClassA* );
}

//ClassMain.cpp
#include ClassMain.h
#include ClassA.h         //Order of these two doesn't mater

ClassMain::ClassMain()
{
    m_pA = NULL;
}

ClassMain::ClassMain( ClassA* pA )
{
    m_pA = pA;
}

Open in new window


OF course you have to be careful regarding the life cycle of ClassMain and ClassA.  If you create a ClassA object and a ClassMain object, and then pass a pointer to the ClassA object to ClassMain, and ClassA becomes invalid (such as goes out of scope), then the pointer in ClassMain becomes invalid.  This is where creating the ClassA object within ClassMain can be safer, because that way as the programmer of ClassMain, you control the life cycle of ClassA.  Otherwise, passing a pointer into ClassMain means that someone else could use your ClassMain inappropriately.
0
 

Author Comment

by:deleyd
ID: 39681366
If I instantiate ClassA in my ClassMain.h file, is there a problem with everyone who does #include "ClassMain.h" at the top of their file ends up instantiating another instance of ClassA, a side effect they don't necessarily want?
#pragma once
#include "ClassA.h"

class ClassMain
{
public:
	ClassMain(void);
	ClassA a;
};

Open in new window

0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 500 total points
ID: 39681461
you don't instantiate a member variable in the header file but you only declare it.

the member variable a is instantiated when some one does

ClassMain m;

Open in new window



or

ClassMain pm = new ClassMain();

Open in new window


both statements would instantiate a object of class ClassMain and with that also a object of ClassA which could be accessed by m.a in the first case and by pm->a in the pointer case.

Note, if you would make the member a private member (like HooKooDooKu showed) only member functions of ClassMain could be access the member a.

Sara
0
 

Author Closing Comment

by:deleyd
ID: 39682350
I'm starting to get it. Just including ClassMain.h doesn't instantiate an an instance of that class, it just gives information about the class in case anyone's interested. Creating the instance of ClassMain gives me variable a which is an instance of ClassA, and I don't have to do anything in the constructor of ClassMain to create an instance of ClassA

In C# this wouldn't work because a is never created.:
    class ClassMain
    {
        ClassA a;

        public void Run()
        {
            a.Test();
        }
    }

Open in new window

But in C++ this works just fine:
class ClassMain
{
public:
  ClassA a;

  void Run()
  {
    a.Test();
  }
};

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

764 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