I have a class which takes two arguments on it’s constructor, an int and a void(*)(void) so normally when I need to call it I do it like this:
obj_child (int_value, pointer_to_foo);
Now what I want is to instantiate the obj_child with constant arguments, within another class.
So I tried:
class obj_parent
{
private:
obj_child child_instantiation (int_value, pointer_to_foo);
};
but this seems to give me two compiler errors on the line I declare child_instantiation so I guess the arguments can’t be passed there but somewhere else.
Mind you child_instantiations are supposed to have the same arguments for all obj_parent instantiations, so they shouldn’t be passed as obj_parent constructor arguments.
Declaring the class pointer and then creating a new one on the heap compiles, but I don’t want to do it that way and I don’t know if it works (my debugger can’t watch the reference so it’s very hard to monitor it’s values).
class obj_parent
{
private:
obj_child *child_instantiation;
};
obj_parent::
obj_parent (void)
{
child_instantiation = new obj_child child_instantiation (int_value, pointer_to_foo);
}
Thanks!
(Please don’t mind the semantics, child – parent has nothing to do with inheritance, just couldn’t think better names right now)
You have to initialize the object in the constructor of the class.