Instantiating classes within classes
-
I am trying to figure out how to instantiate a class that contains another class. For example, if I have a class called TopClass that contains a class called InClass : class TopClass{ TopClass(); TopClass(int xIn); InClass x; } class InClass{ InClass(); InClass(int xIn); int xIn; } Even when I use non-default constructor in TopClass and called the equivalent one in InClass, it seems that the default construct in InClass has already been called to create a vanilla object. Is there any way I can automatically transfer the input variables to InClass when the non-default constructor is created? Or should I simply use pointers and initialize the pointer to a new InClass object the non-default constructor is called in TopClass? Thanks in advance for any info. Kiernan
-
I am trying to figure out how to instantiate a class that contains another class. For example, if I have a class called TopClass that contains a class called InClass : class TopClass{ TopClass(); TopClass(int xIn); InClass x; } class InClass{ InClass(); InClass(int xIn); int xIn; } Even when I use non-default constructor in TopClass and called the equivalent one in InClass, it seems that the default construct in InClass has already been called to create a vanilla object. Is there any way I can automatically transfer the input variables to InClass when the non-default constructor is created? Or should I simply use pointers and initialize the pointer to a new InClass object the non-default constructor is called in TopClass? Thanks in advance for any info. Kiernan
I'm not certain I understand correctly but will this not work ?
/// this should for the appropriate constructor of X to be called.
TopClass::TopClass( int xIn ) : x( xIn )
{
}
Maximilien Lincourt Your Head A Splode - Strong Bad
-
I am trying to figure out how to instantiate a class that contains another class. For example, if I have a class called TopClass that contains a class called InClass : class TopClass{ TopClass(); TopClass(int xIn); InClass x; } class InClass{ InClass(); InClass(int xIn); int xIn; } Even when I use non-default constructor in TopClass and called the equivalent one in InClass, it seems that the default construct in InClass has already been called to create a vanilla object. Is there any way I can automatically transfer the input variables to InClass when the non-default constructor is created? Or should I simply use pointers and initialize the pointer to a new InClass object the non-default constructor is called in TopClass? Thanks in advance for any info. Kiernan
<naughty>I see you skipped the chapter on initialisation lists when you were learning C++</naughty>. Well to be honest, it IS an obscure topic and quite easy to miss when you're learning C++. So I'll let you off... THIS time. Anyway, the answer is to use an initialisation list.
class TopClass()
{
TopClass(int xIn)
: x(xIn)
/** The above line tells the compiler to initialise
x to xIn, using the constructor that takes an int.
If you don't provide an initialisation list, the
default constructor is used **/
{
}
InClass x;
};-- modified at 10:24 Monday 20th March, 2006
-
<naughty>I see you skipped the chapter on initialisation lists when you were learning C++</naughty>. Well to be honest, it IS an obscure topic and quite easy to miss when you're learning C++. So I'll let you off... THIS time. Anyway, the answer is to use an initialisation list.
class TopClass()
{
TopClass(int xIn)
: x(xIn)
/** The above line tells the compiler to initialise
x to xIn, using the constructor that takes an int.
If you don't provide an initialisation list, the
default constructor is used **/
{
}
InClass x;
};-- modified at 10:24 Monday 20th March, 2006
Thank you for the help. I guess I did skip that chapter. I am learning C++ after having learned Java, so this is a subtlety that escaped my attention since this problem doesn't really occure in Java due to its lack of definitions. You answer does however bring me to another question. Isn't that the same syntax used used for passing variables to an inherited version of a function? Did I miss the big picture, whereby that syntax basically allows me to pass values to any other valid function? Regards Kiernan
-
Thank you for the help. I guess I did skip that chapter. I am learning C++ after having learned Java, so this is a subtlety that escaped my attention since this problem doesn't really occure in Java due to its lack of definitions. You answer does however bring me to another question. Isn't that the same syntax used used for passing variables to an inherited version of a function? Did I miss the big picture, whereby that syntax basically allows me to pass values to any other valid function? Regards Kiernan
-
Thank you for the help. I guess I did skip that chapter. I am learning C++ after having learned Java, so this is a subtlety that escaped my attention since this problem doesn't really occure in Java due to its lack of definitions. You answer does however bring me to another question. Isn't that the same syntax used used for passing variables to an inherited version of a function? Did I miss the big picture, whereby that syntax basically allows me to pass values to any other valid function? Regards Kiernan
That syntax is only valid (AFAICT) in a constructor. The syntax is used to initialise any base classes, and any members. By the time you enter the function, all external parameters need to have been initialized (base classes, members, and global variables). Although we already have syntax to initialise global variables, this is the only syntax available to initialise members and base classes.