If I have a class ABC for example. What's the different between these calls?
Code:
class ABC{
ABC(){
}
}
You missed a semi-colon at the end of the class definition.
Originally Posted by cpthk
ABC var1;
This creates a stack object of type ABC.
Originally Posted by cpthk
ABC var2();
This is the declaration of a function named var2 which has no parameters an returns an ABC object by value.
Originally Posted by cpthk
ABC var3 = new ABC();
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:
Code:
ABC * var3 = new ABC();
And since you started it, there's also...
Code:
ABC * var4 = new ABC;
Which for non-POD (plain old data) structures is basically the same thing as the third case.
No. This is just a function declaration. ( see prev. posts ).
you propably want
Code:
ABC var2;
Kurt
So what if I have two constructor
Code:
class ABC{
ABC(){
...
}
ABC(int i){
...
}
};
Code:
ABC varA;
This calls default constructor.
Code:
ABC varB(1);
This calls the constructor that takes an integer.
Code:
ABC varC();
This is a varC function declaration that returns object of type ABC.
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.
This is a varC function declaration that returns object of type ABC.
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.
Yes, you've got it. It might be confusing at first, but you'll get used to it. That's C++ parsing.
Bookmarks