Thomas
Thomas

Reputation: 6196

What type of syntax is being used in this declaration?

SomeSmartPtr<MyObject> ptr(new MyObject());
ptr->DoSomething(); // Use the object in some way.

I'm just a c#/java person. What type of syntax is being used in the first line with the function call. Why is there not a declaration followed by the assignment operator. What allows you to assign and declare like that.

Upvotes: 0

Views: 61

Answers (1)

R Sahu
R Sahu

Reputation: 206567

The first form of declaring and initializing an object is called direct initialization.

You can also use:

SomeSmartPtr<MyObject> ptr = new MyObject();

It is called copy initialization. You can read about the various types of initialization supported by C++ at http://en.cppreference.com/w/cpp/language/initialization.

Upvotes: 3

Related Questions