tags:

views:

474

answers:

4

I'm trying to write a class that when asked on, will call on a class and make it into a class member. Here's a quick example of what I mean:

class foo{
  myClass Class;
  foo(); 
};
foo::foo()
{
  //Create the class and set it as the foo::Class variable
}

I'm sure this is actually an easy thing to do. Any help would be appreciated Thanks

Edit: Sorry for the bad terminology. I'll try to go over what I want to do more concisely. I'm creating a class(foo), that has a varaible(Class) with the class definition of (myClass). I need to create a myClass object and assign it to Class, with-in the class foo. Through the help of everyone, I have kind of achieved this.

My problem now is that the object I created gives me a "Unhandled Exception"..."Access violation reading location 0x000000c0" in Class, on the first line of myClass's function that I'm trying to execute. Thanks for your help!

note: I'm currently using emg-2's solution.

A: 

If you only want to composite, use Neil solution. If you want an aggregation (i.e. you will assign outside objects to myClass*Class), you should use a pointer:

class foo{
  myClass * Class;
  void createClass(); 
};
// can't use name foo, because its a name reserved for a constructor
void foo::createClass() {
  Class = new myClass();
}
I did this, and it compiled but I got a run time error when the class changed a member variable.
Sam
@Sam: it looks like the error is somewhere else, not in the technique used to store a class inside a class.
It's because his variable is an object rather than a pointer
ChrisF
It's possible, but normally new object should be copied and the old object overwritten.
Let me explain what I meant more clearly :)if I ran Class->doSomething(); inside foo::createClass and gave you an error on that function, on the first thing it did, then what do you think would cause an issue like that? (or what have I done wrong?) My current code is using your solution. Thanks
Sam
That sounds like the member function doSomething has been called with an invalid object. In that case, you'd have a garbage this pointer and you'd expect a crash the first time it tried to access any member data or call a virtual function. Are you calling the function before you have created the object?
markh44
+1  A: 

I am not sure I understood the question correctly. But if you are trying to create an object on demand then you can do something like this:

class foo{
  myClass* m_pClass;
  foo();
  myClass* f();
  ~foo();
};

foo::foo() : m_pClass(NULL) //Initialize the pointer to NULL do not create any object in the constructor
{
}

foo::~foo()
{
  //Release the allocated object
  delete m_pClass;
  m_pClass = NULL;
}

myClass* foo::f()
{
  //Create a new object and return
  m_pClass = new myClass;
  return m_pClass;
}
Naveen
Using auto_ptr would be better and returning a reference from foo::f() would also be better. It's amazing how much cleaner code becomes by avoiding naked pointers when possible.
markh44
A: 

Try one of these:

  1. Make Class a pointer to myClass, and dynamically create it in the c'tor
  2. Use myClass c'tor to set it in foo's initialization list
  3. Add an Init(...) to myClass and use it in foo's c'tor
  4. Tell us exactly what are you trying to achieve...
eran
+4  A: 

There is no need to do anything. Assuming that myClass has a default constructor, it will be used to construct the Class (you could use better names here, you know) instance for you. If you need to pass parameters to the constructor, use an initialisation list:

foo :: foo() : Class( "data" ) {
}
anon
Yeah, it's probably what he really intended to do :) A composition.