Avatar of deleyd
deleyd
Flag 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.)
C++

Avatar of undefined
Last Comment
deleyd

8/22/2022 - Mon
SOLUTION
chaau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
chaau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
HooKooDooKu

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23