class *obj = new class;

V

_vjy

class class1 {};
class1 *obj = new class1;

is this valid syntax ? shouldn't this be like,

class1 *obj = new class1();

g++ gives no errors / warnings.
 
B

Bo Persson

_vjy said:
class class1 {};
class1 *obj = new class1;

is this valid syntax ? shouldn't this be like,

class1 *obj = new class1();

g++ gives no errors / warnings.

It doesn't matter much in your example. The first one will be default
initialized, while the second is value initialized. For an empty class
object, there is no difference.

On the other hand

int* one = new int;
int* two = new int();

will cause 'one' to point to an uninitialized int, while 'two' points
to an int that is zero.


If you have a class object with a constructor, the constructor will be
called in both cases.



Bo Persson
 
J

James Kanze

It doesn't matter much in your example. The first one will be
default initialized, while the second is value initialized.
For an empty class object, there is no difference.
On the other hand
int* one = new int;
int* two = new int();
will cause 'one' to point to an uninitialized int, while 'two' points
to an int that is zero.
If you have a class object with a constructor, the constructor
will be called in both cases.

There's no such thing as a class type without a constructor. If
the class has a trivial default constructor (the one provided by
the compiler), on the other hand, the distinction you mention
holds, with default initialization leaving members of built-in
types uninitialized, and value initialization zero-initializing
them.
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top