0

String is an object, but it can be initialize without (), like

string str;

However if I define a user defined object, I must initialize it like:

myclass myobj();

How is it possibel to not use: () ?

4
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by firstPerson
0

No you don't have to use () on your own c++ classes.

class MyClass
{
public:
    MyClass() { cout << "Hello World\n"; }
};

int main()
{
    MyClass c;
}
0

>However if I define a user defined object, I must initialize it like:
>myclass myobj();

Actually, that example is wrong. Due to some grammar issues, myobj in your example is actually a function declaration, not an object of myclass. When creating objects using the default constructor, you don't include a parameter list:

myclass myobj;
0

String is an object, but it can be initialize without (), like

string str;

However if I define a user defined object, I must initialize it like:

myclass myobj();

How is it possibel to not use: () ?

The compiler will most likely mistake myobj as a function!

As Narue said, for default constructors, you do NOT need the
"()" after the object creation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.