473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Object initialisation and constructors

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

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

"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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can...
106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
2
by: newbiecpp | last post by:
I have a simple class: class Point { public: Point() : xval(0), yval(0) {} Point(int x, int y) : xval(x), yval(0) {} private: int xval, yval; };
4
by: Christof Warlich | last post by:
Hi, this is an STL newbe question, sorry if it is too stupid: Is there a way to statically _initialize_ an STL map, i.e. not at runtime? There seems to be a map constructor that expects a...
6
by: Charles Law | last post by:
As a matter of practice, where would people put the following elements of object creation/initialisation: Create shared member objects Initialise shared member objects Create non-shared member...
13
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e....
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
5
by: mackenzie | last post by:
Hello, I am looking for a little bit of help. I am trying to create a dynamically allocated object which contains one or more objects of type boost::pool<>. I get a compiler error when an object...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.