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?
Re: What is the difference between these object instantiation methods?
The latter declares a function that takes no arguments and returns a ClassName.
Re: What is the difference between these object instantiation methods?
Quote:
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
Re: What is the difference between these object instantiation methods?
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!