-
August 6th, 2009, 03:47 PM
#1
difference between calling constructors?
If I have a class ABC for example. What's the different between these calls?
Code:
class ABC{
ABC(){
}
}
ABC var1;
ABC var2();
ABC var3 = new ABC();
I know var3 gets a pointer to the class ABC in heap. What about var1 and var2?
Thanks.
-
August 6th, 2009, 03:53 PM
#2
Re: difference between calling constructors?
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:
This will result in a compile error:
Code:
ABC var3 = new ABC();
You probably meant to write:
Code:
ABC* var3 = new ABC();
-
August 6th, 2009, 04:03 PM
#3
Re: difference between calling constructors?
Originally Posted by cpthk
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.
-
August 7th, 2009, 04:30 PM
#4
Re: difference between calling constructors?
does var2 call the constructor in the class?
-
August 7th, 2009, 05:40 PM
#5
Re: difference between calling constructors?
No. This is just a function declaration. ( see prev. posts ).
you propably want
Kurt
-
August 7th, 2009, 05:50 PM
#6
Re: difference between calling constructors?
Originally Posted by ZuK
No. This is just a function declaration. ( see prev. posts ).
you propably want
Kurt
So what if I have two constructor
Code:
class ABC{
ABC(){
...
}
ABC(int i){
...
}
};
This calls default constructor.
This calls the constructor that takes an integer.
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.
-
August 7th, 2009, 07:00 PM
#7
Re: difference between calling constructors?
Originally Posted by cpthk
So what if I have two constructor
Code:
class ABC{
ABC(){
...
}
ABC(int i){
...
}
};
This calls default constructor.
This calls the constructor that takes an integer.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|