If I have a class ABC for example. What's the different between these calls?
ABC var1;Code:class ABC{
ABC(){
}
}
ABC var2();
ABC var3 = new ABC();
I know var3 gets a pointer to the class ABC in heap. What about var1 and var2?
Thanks.
Printable View
If I have a class ABC for example. What's the different between these calls?
ABC var1;Code:class ABC{
ABC(){
}
}
ABC var2();
ABC var3 = new ABC();
I know var3 gets a pointer to the class ABC in heap. What about var1 and var2?
Thanks.
This creates an object, named var1, of type ABC:
This declares a function, named var2, that takes no arguments and returns an object of type ABC:Quote:
ABC var1;
This will result in a compile error:Code:ABC var2();
You probably meant to write:Code:ABC var3 = new ABC();
Code:ABC* var3 = new ABC();
You missed a semi-colon at the end of the class definition.
This creates a stack object of type ABC.
This is the declaration of a function named var2 which has no parameters an returns an ABC object by value.
As you said, this creates an ABC object in the free store - sometimes also called the heap, but I really don't want to get into a philosophical discussion about memory area names :). Just notice that you forgot the * indicating a pointer. The correct form would be:
And since you started it, there's also...Code:ABC * var3 = new ABC();
Which for non-POD (plain old data) structures is basically the same thing as the third case.Code:ABC * var4 = new ABC;
does var2 call the constructor in the class?Code:ABC var2();
No. This is just a function declaration. ( see prev. posts ).
you propably want
KurtCode:ABC var2;
So what if I have two constructor
Code:class ABC{
ABC(){
...
}
ABC(int i){
...
}
};
This calls default constructor.Code:ABC varA;
This calls the constructor that takes an integer.Code:ABC varB(1);
This is a varC function declaration that returns object of type ABC.Code:ABC varC();
Is this what you guys meant? Is so, varA and varB are kind of confusing, since you need "()" for constructor that takes parameters but you do not need "()" at all if constructor do not take parameters.