Object initialisation and constructors

M

Martin

Bit of a newbie question here (I'm familiar with OO in general, just
not in C++ yet).

I've seen code example where the general syntax for creating a new
object is as follows:

Thing a = new Thing(arg1, arg2);

and this returns a pointer to a new object of class 'Thing', as well
as calling its constructor.

However, it also seems like it's valid to just write this:

Thing a (arg1, arg2);

and then start using it. What is/are the differences, if any? Any
information much appreciated.
 
M

Mike Wahler

Martin said:
Bit of a newbie question here (I'm familiar with OO in general, just
not in C++ yet).

I've seen code example where the general syntax for creating a new
object is as follows:

Thing a = new Thing(arg1, arg2);

operator new returns a pointer.

Thing *a = new Thing(arg1, arg2);
and this returns a pointer to a new object of class 'Thing', as well
as calling its constructor.
Yes.


However, it also seems like it's valid to just write this:

Thing a (arg1, arg2);

and then start using it.
Yes.

What is/are the differences, if any?

The first form has 'allocated' storage duration.
The object must be specifically destroyed (with 'delete').

The second has 'static' or 'automatic' storage duration,
depending upon scope. It will be automatically destroyed.

-Mike
 
M

Martin

Mike Wahler said:
The first form has 'allocated' storage duration.
The object must be specifically destroyed (with 'delete').

The second has 'static' or 'automatic' storage duration,
depending upon scope. It will be automatically destroyed.

-Mike

Thanks for clearing that up. Given the above, why would you ever want
to use the 'new' syntax, when using the other form will handle garbage
collection for you?

I suppose it might be handy to be able to get a pointer straight back,
but if you needed one, you could surely do

Thing t(a,b);
Thing *p_t = &t;

Or does this incur a significant overhead?
 
C

Christoph Rabel

Martin said:
Thanks for clearing that up. Given the above, why would you ever want
to use the 'new' syntax, when using the other form will handle garbage
collection for you?

I suppose it might be handy to be able to get a pointer straight back,
but if you needed one, you could surely do

Thing t(a,b);
Thing *p_t = &t;

Or does this incur a significant overhead?

No overhead, but consider this:

Thing* foo()
{
Thing t(a,b);
Thing *p_t = &t;
return p_t;
} // t gets destroyed here(!)

int main()
{
Thing* t = foo();
}

In the above example the object t is destroyed and ceases to
exist when the function ends.

Now, in main, you get from the function a pointer to an
object that doesnt exist anymore.

Automatic variables have a scope, and when this scope ends
they are destroyed. With new, you control when the object is
destroyed. But with the control, you also have the
responsibility to delete it yourself.

hth,

Christoph
 
J

jeffc

Martin said:
Bit of a newbie question here (I'm familiar with OO in general, just
not in C++ yet).

I've seen code example where the general syntax for creating a new
object is as follows:

Thing a = new Thing(arg1, arg2);

and this returns a pointer to a new object of class 'Thing', as well
as calling its constructor.

However, it also seems like it's valid to just write this:

Thing a (arg1, arg2);

and then start using it. What is/are the differences, if any? Any
information much appreciated.

You've already pointed out the differences. Basically it boils down to
either the need or desire for dynamic vs. static creation, or personal
preference.
 
J

jeffc

Martin said:
"Mike Wahler" <[email protected]> wrote in message

Thanks for clearing that up. Given the above, why would you ever want
to use the 'new' syntax, when using the other form will handle garbage
collection for you?

Again, read up on dynamic memory allocation. How can you create those
variables if you don't know how many you need when you write the program?
What if creating them is based on some user input, that you can't predict
until the program is executing?
 
M

Martin

Christoph Rabel said:
No overhead, but consider this:

Thing* foo()
{
Thing t(a,b);
Thing *p_t = &t;
return p_t;
} // t gets destroyed here(!)

int main()
{
Thing* t = foo();
}

In the above example the object t is destroyed and ceases to
exist when the function ends.

Now, in main, you get from the function a pointer to an
object that doesnt exist anymore.

Automatic variables have a scope, and when this scope ends
they are destroyed. With new, you control when the object is
destroyed. But with the control, you also have the
responsibility to delete it yourself.

hth,

Christoph

Thanks for the example - that makes sense. This has made me think of one
more thing (and my newbie-ness may be shining through like a beacon here,
but anyway...) - I understand that references, unlike pointers, have to
point to a valid variable (eg: not null). If I were to have a similar
situation to your example, but returned a reference instead (see below),
would the reference in main() still be valid?

Thing& foo()
{
Thing t(a,b);
Thing &t_ref = t;
return t_ref;
} // t gets destroyed here(!)

int main()
{
Thing& t = foo();
}

Is t a valid reference in main() (and does it still refer to a valid
object), or is this just completely stupid/invalid code ?
 
R

red floyd

Martin said:
Thanks for the example - that makes sense. This has made me think of one
more thing (and my newbie-ness may be shining through like a beacon here,
but anyway...) - I understand that references, unlike pointers, have to
point to a valid variable (eg: not null). If I were to have a similar
situation to your example, but returned a reference instead (see below),
would the reference in main() still be valid?

Thing& foo()
{
Thing t(a,b);
Thing &t_ref = t;
return t_ref;
} // t gets destroyed here(!)

int main()
{
Thing& t = foo();
}

Is t a valid reference in main() (and does it still refer to a valid
object), or is this just completely stupid/invalid code ?

same problem as returning a pointer to a local. The reference is no longer valid upon return.
 

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

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top