faq new as parameter in the constructor

F

fcvcnet

Hi,

CarPtr g()
{
CarPtr p(new Car()); <-- How to explain this
// ...
return p;
}
Thanks.
 
B

benben

fcvcnet said:
Hi,

CarPtr g()
{
CarPtr p(new Car()); <-- How to explain this

CarPtr is a class. The above statement constructs a CarPtr object named
p with a parameter that is a raw pointer to a dynamically allocated Car
object.
 
V

Victor Bazarov

fcvcnet said:
Hi,

CarPtr g()
{
CarPtr p(new Car()); <-- How to explain this
// ...
return p;
}

What's there to explain? It's called "initialisation". You may
have seen it before. You can even do that with built-in types,
like a double:

double pi(3.1415926);

V
 
R

Rolf Magnus

benben said:
CarPtr is a class.

It is? How do you now that?
The above statement constructs a CarPtr object named p with a parameter
that is a raw pointer to a dynamically allocated Car object.

Yes. However, CarPtr could also be a typedef for Car*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top