|
-
March 4th, 2011, 01:12 PM
#1
What is the difference between these object instantiation methods?
What is the difference between these object instantiation methods:
1.
Code:
ClassName ObjectName;
2.
Code:
ClassName ObjectName();
Is there any?
-
March 4th, 2011, 01:17 PM
#2
Re: What is the difference between these object instantiation methods?
The latter declares a function that takes no arguments and returns a ClassName.
-
March 4th, 2011, 03:06 PM
#3
Re: What is the difference between these object instantiation methods?
 Originally Posted by Feoggou
What is the difference between these object instantiation methods:
There is no instantiation going on in the second method. That's a function declaration.
See here:
Code:
#include <string>
int main()
struct ClassName
{
};
ClassName ObjectName(); // declare function
int main()
{
ClassName theName;
theName = ObjectName();
}
// here is the function impelemtation
ClassName ObjectName()
{
return ClassName();
}
See the code in red?
Regards,
Paul McKenzie
-
March 5th, 2011, 01:29 AM
#4
Re: What is the difference between these object instantiation methods?
-
March 5th, 2011, 09:46 AM
#5
Re: What is the difference between these object instantiation methods?
thanks a lot.
It seems that if you have a class defined like this:
Code:
class ClassName
{
int x;
public:
ClassName()
{
x = 3;
}
};
you can use either this:
or this:
And I was saying to myself: there can't be any difference!
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
|