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

Create class using new?

P: n/a
mlt
I have the following class:

class Test {

public:
Test() {
a = 33;
}

private:
int a;
};
When I want to create an instance of this class in eg java I do:

Test t = new Test();

but in C++ I either do:

1) Test t;
or
2) Test t();

(what are the difference between 1 and 2?).

I can't do:

Test t = new Test();

but is that not the only way to make sure the instance lives after the
calling function has returned?

Nov 21 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Nov 21, 8:29*pm, "mlt" <a...@asd.comwrote:
but is that not the only way to make sure the instance lives after the
calling function has returned?
Option 1 and 2 are equal. If a class has a default constructor the ()
behind the identifier can be omitted. In C++ you can also do

Test* t = new Test();

The difference with your options is that in this case a Test object is
dynamically created and t is a Test pointer that points to that
object. This is indeed the only way to make sure the instance lives
after the function returned, but keep in mind that in C++ dynamically
created objects are not freed automatically. You will have to
explicitly call delete on a pointer to the object. Eg.

Test* t = new Test();
// Some code.
delete t;

If you don't, the object t points to will never be deleted and you
have a memory leak. I suggest you to read a good book on C++ if these
things are unfamiliar to you.
Nov 21 '08 #2

P: n/a
On Nov 21, 12:01*pm, Tonni Tielens <tonnitiel...@gmail.comwrote:
On Nov 21, 8:29*pm, "mlt" <a...@asd.comwrote:
The options were

1) Test t;
or
2) Test t();
Option 1 and 2 are equal.
That never fails to catch us off guard: Option 2 is the declaration of
a function. :)

Ali
Nov 21 '08 #3

P: n/a
On Nov 21, 2:29 pm, "mlt" <a...@asd.comwrote:
I have the following class:

class Test {

public:
Test() {
a = 33;
}

private:
int a;

};

When I want to create an instance of this class in eg java I do:

Test t = new Test();

but in C++ I either do:

1) Test t;
or
2) Test t();

(what are the difference between 1 and 2?).

I can't do:

Test t = new Test();
new returns a pointer, since t is an instance of Test, you can't store
a pointer (to an allocated Test) into it, thankfully. This works:

Test* p_t = new Test();
....
delete p_t; // required

It is unwise to allocate on the heap unless absolutely necessary. And
when it is necessary its better to rely on RAII or smart pointers.
So this is better since it will allocate + invoke ctor + get
destructed automatically.

Test t; // allocated on the stack, not heap
>
but is that not the only way to make sure the instance lives after the
calling function has returned?
No, definitely not the way. Its shoddy programming to allocate
something over there and deallocate somewhere else. Managing the
lifetime of an object should either be automatic or given to a single
manager. In Java, your GC takes care of that while C++ uses a
different discipline. Java only uses the stack to store primitives, C+
+ can use the stack to store anything.

Nov 21 '08 #4

P: n/a
mlt

<ac******@gmail.comwrote in message
news:00**********************************@w1g2000p rk.googlegroups.com...
On Nov 21, 12:01 pm, Tonni Tielens <tonnitiel...@gmail.comwrote:
On Nov 21, 8:29 pm, "mlt" <a...@asd.comwrote:
The options were

1) Test t;
or
2) Test t();
Option 1 and 2 are equal.
That never fails to catch us off guard: Option 2 is the declaration of
a function. :)

Ali

Ok so:

Test t();

actually declares a function that returns a type Test, which could be
implemented/defined in e.g a .cpp file? But what about:
Test* t = new Test();

versus:

Test* t = new Test;

that would create the same pointer right?

Nov 21 '08 #5

P: n/a
On Nov 21, 12:46 pm, "mlt" <a...@asd.comwrote:
But what about:

Test* t = new Test();

versus:

Test* t = new Test;

that would create the same pointer right?
The short answer is: yes, in both cases, t points to an object
allocated on the heap.

But there will be a difference whether Test is a POD or not. The rules
are too complicated for me to remember. I need to do a search
everytime I wonder the behavior.

In this case, if Test is non-POD, the two are the same and the object
is default constructed. But if Test is a POD, then the second will not
"zero-initialize" the object.

A search for "is there a difference between new MyClass; and new
MyClass()" in this group finds a thread about the same issue with many
more responses.

Ali
Nov 21 '08 #6

P: n/a
"mlt" <as**@asd.comkirjutas:
I have the following class:

class Test {

public:
Test() {
a = 33;
}

private:
int a;
};
When I want to create an instance of this class in eg java I do:

Test t = new Test();

but in C++ I either do:

1) Test t;
or
2) Test t();

(what are the difference between 1 and 2?).
1) defines a default-constucted variable t of type Test.

2) declares a function t(), taking no arguments and returning a Test
object by value (welcome to the wonderful world of C++ cryptic
declaration syntax!).
>
I can't do:

Test t = new Test();
You probably want

Test* t = new Test();

In C++, the types Test* (a pointer) and Test (a class) are different.

However, a raw pointer is usually not a good way to manage the ownership
of the created object, it may get lost too easily in presence of
exceptions, and there is no garbage collection present by default. A
better way might be to use std::auto_ptr or boost::shared_ptr.
>
but is that not the only way to make sure the instance lives after the
calling function has returned?
Instances are needed only for "entity" objects. For "value" objects it is
normal to return them by value from functions, just like int. BTW, even C
allows returning structs by value.

hth
Paavo
Nov 21 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.