Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

How to instantiate class within a class?

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.)
SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

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