Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
class MyClass
{
public:
	MyClass() { cout<<"Create object"<<endl; }	
};
void main()
{ 

	MyClass obj2(); //why when i write 2 brackets after name, object not created
	MyClass obj1;   //this statment create object
}
Posted

I doubt that only one of the objects is being created. Let's modify your code in the following way:
C++
static int objId = 0;

class MyClass
{
public:
	MyClass() { cout << "Create object "<< objId  << endl; }	
};

void main()
{ 
        objId = 2;
	MyClass obj2(); //why when i write 2 brackets after name, object not created

        objId = 1;
	MyClass obj1;   //this statment create object
}

Now you can distinguish whether the first or the second call or both are being executed.

[AMENDED]
Apparently the C++ compiler interprets the first one (obj2) as a function prototype declaraction. It thinks you are declaring a function with name obj2 that takes no parameters and returns a value of type MyClass.

When I compile it with VC++ the compiler generates the warning:
prototyped function not called (was a variable definition intended?)

which indicates what's going on. If you would create an object via new, this syntax would be allowed, i.e:
C++
MyClass* pObj2 = new MyClass();

Hope that helped clarify it.
 
Share this answer
 
v2
Comments
Anderso0on 20-Oct-13 12:17pm    
ok , i say that in my post , i know the object with ID=2 does not work , but why ??
Andreas Gieriet 20-Oct-13 15:50pm    
See my Solution#3.
Cheers
Andi
Sergey Alexandrovich Kryukov 20-Oct-13 23:16pm    
5ed.
—SA
Came across the same problem once.

The reason is MyClass obj2() does not create an object of MyClass. It is a function prototype declared locally that returns a MyClass object.


If you want to create an object of MyClass, use this
C++
MyClass obj1; //withouts brackets
 
Share this answer
 
v2
Comments
enhzflep 20-Oct-13 12:47pm    
Or this: MyClass *obj1 = new MyClass(constructerParamsGoHere);
Captain Price 20-Oct-13 12:51pm    
That's fine. It creates a new object of MyClass even there're no constructor parameters, but only brackets.
enhzflep 20-Oct-13 13:15pm    
Gets my vote. :)
Captain Price 20-Oct-13 13:16pm    
thanks enhzflep !
enhzflep 20-Oct-13 13:28pm    
You're welcome. :)
The syntax of many languages have ambiguities that need resolution.

The given case is an ambiguity that is explicitly resolved in the C++ language specification.
The resolutions basically says: what can be a declaration is taken as a declaration.

E.g.
C++
MyType f(); // function declaration and not object definition
MyType f;   // object definition

Cheers
Andi

PS: If you are interested in language spec and in particular in ambiguities of C++ and their resolution, have a look at the Working Draft, Standard for Programming Language C++[^] and search for ambiguity ;-) - this is not the final C++11 spec but for ambiguity discussions it's sufficient...
 
Share this answer
 
v4
Comments
nv3 20-Oct-13 15:07pm    
Thanks, Andi. My 5.
Andreas Gieriet 20-Oct-13 15:17pm    
Thank you for your 5!
Cheers
Andi
Sergey Alexandrovich Kryukov 20-Oct-13 23:14pm    
My 5.
—SA
Andreas Gieriet 21-Oct-13 2:18am    
Hello Sergey,
Thank you for your 5!
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900