variable declaration

W

widmont

Hello,

I would like to know the difference between two variable declaration
ways:

int point(0);

and

int point=0;

Thanks for your help!!

A.
 
T

Tomás

widmont posted:
Hello,

I would like to know the difference between two variable declaration
ways:

int point(0);

and

int point=0;

Thanks for your help!!

A.

When you're working with intrinsic types, there's no difference whatsoever.
It's a situation of "Thanks" Vs. "Thank you". Pick whichever tickles your
fancy.

Be careful though. If you want to "default initialise" something, then you
can't simply write:

int point();

That my friend is a function declaration. You can work your way around this
with:

int point = int();

But unfortunately, the type you're working with must have a public copy
constructor. So it won't work with "ostringstream":

ostringstream blah = ostringstream();


Fortunately though, if your type is a POD, you can do this:

PodType object = {};

(I'm open to correction on whether the above actually zero initializes
everything. If memory serves me right, then it does.)

When you have a contructor which takes more than one argument, then you
*have* to use the parenthesis form, eg.:

Dog benji("Benji",7);

You can't do:

Dog benji = "Benji", 7;


If you want to know how to default initialise something that has a non-
public copy constructor, then:

template<class T>
class DefaultInitialised : public T
{

DefaultInitialised() : T()
};

int main()
{
DefaultInitialised<ostringstream> k;
}


-Tomás
 
V

Victor Bazarov

widmont said:
I would like to know the difference between two variable declaration
ways:

int point(0);

and

int point=0;

In this particular case (with 'int'), none.

V
 
R

Rolf Magnus

Tomás said:
widmont posted:


When you're working with intrinsic types, there's no difference
whatsoever. It's a situation of "Thanks" Vs. "Thank you". Pick whichever
tickles your fancy.

Be careful though. If you want to "default initialise" something, then you
can't simply write:

int point();

That my friend is a function declaration. You can work your way around
this with:

int point = int();

But unfortunately, the type you're working with must have a public copy
constructor. So it won't work with "ostringstream":

ostringstream blah = ostringstream();

No, but you can simply do:

ostringstream blah;

This will give you a default initialized variable, if the type is non-POD.
Fortunately though, if your type is a POD, you can do this:

PodType object = {};

I think this only works for compound types.
(I'm open to correction on whether the above actually zero initializes
everything. If memory serves me right, then it does.)

It default-initializes everything, which means zero for integer types.
When you have a contructor which takes more than one argument, then you
*have* to use the parenthesis form, eg.:

Dog benji("Benji",7);

You can't do:

Dog benji = "Benji", 7;


If you want to know how to default initialise something that has a non-
public copy constructor, then:

template<class T>
class DefaultInitialised : public T
{

DefaultInitialised() : T()
};

That constructor is private.
 
M

Marcus Kwok

Tom?s said:
When you have a contructor which takes more than one argument, then you
*have* to use the parenthesis form, eg.:

Dog benji("Benji",7);

You can't do:

Dog benji = "Benji", 7;

However, you can do:

Dog benji = Dog("Benji", 7);

(assuming the appropriate copy-constructors, etc., are accessible).
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top