#include <iostream> class MyClass {Public:myclass () {std::cout << "Hello myclass!" << Std::endl;} PU Blic:void MyMethod () {std::cout << "Hello mymethod!" << Std::endl;};
If this defines the object: MyClass obj; Then its constructor will be executed if obj is invoked. MyMethod () is also performed correctly.
But if with parentheses: MyClass obj (); Then its constructor is not executed, call obj. MyMethod (), there will also be syntax errors, this syntax is the definition of obj what is.
MyClass obj () defines obj as what is syntactically defined as a parameterless function named obj, the type MyClass class (that is, the return value is of type MyClass).
In addition, if the dynamic heap allocation is to be parenthesized, such as MyClass *obj = new MyClass () does not add an error (but, in the vc6.0 does not add () and no error, and the result is the same, the reason to be solved. However, if a class has a constructor defined with parameters and does not define a default constructor (at which point the compiler no longer generates a default construct), it is certainly wrong to define the class without parentheses #add to avoid errors, uniform braces are wonderful.