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: () ?

Recommended Answers

All 3 Replies

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

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

int main()
{
    MyClass c;
}

>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;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.