By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,908 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,908 IT Pros & Developers. It's quick & easy.

Object initialisation and constructors

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Martin" <ma****@metronet.co.uk> wrote in message
news:3b**************************@posting.google.c om...
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
Jul 22 '05 #2

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<u8*****************@newsread2.news.pas.earth link.net>...
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?
Jul 22 '05 #3

P: n/a
Martin wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<u8*****************@newsread2.news.pas.earth link.net>...

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

Jul 22 '05 #4

P: n/a

"Martin" <ma****@metronet.co.uk> wrote in message
news:3b**************************@posting.google.c om...
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.
Jul 22 '05 #5

P: n/a

"Martin" <ma****@metronet.co.uk> wrote in message
news:3b**************************@posting.google.c om...
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<u8*****************@newsread2.news.pas.earth link.net>...
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?


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?
Jul 22 '05 #6

P: n/a
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
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 ?


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #7

P: n/a
Martin wrote:
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
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 ?


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

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.