C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

variable declaration

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
widmont
Guest





PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: variable declaration Reply with quote



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.
Back to top
Tomás
Guest





PostPosted: Tue Feb 28, 2006 1:06 am    Post subject: Re: variable declaration Reply with quote



Quote:
template<class T
class DefaultInitialised : public T
{

DefaultInitialised() : T()

{

}

Quote:
};

int main()
{
DefaultInitialised<ostringstream> k;
}
Back to top
Tomás
Guest





PostPosted: Tue Feb 28, 2006 1:06 am    Post subject: Re: variable declaration Reply with quote



widmont posted:

Quote:
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
Back to top
Victor Bazarov
Guest





PostPosted: Tue Feb 28, 2006 2:06 am    Post subject: Re: variable declaration Reply with quote

widmont wrote:
Quote:
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
--
Please remove capital As from my address when replying by mail
Back to top
Rolf Magnus
Guest





PostPosted: Tue Feb 28, 2006 3:06 pm    Post subject: Re: variable declaration Reply with quote

Tomás wrote:

Quote:
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();

No, but you can simply do:

ostringstream blah;

This will give you a default initialized variable, if the type is non-POD.

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

PodType object = {};

I think this only works for compound types.

Quote:
(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.

Quote:
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.

Quote:
int main()
{
DefaultInitialised<ostringstream> k;
}
Back to top
Marcus Kwok
Guest





PostPosted: Thu Mar 02, 2006 7:23 pm    Post subject: Re: variable declaration Reply with quote

"Tom?s" <NULL (AT) null (DOT) null> wrote:
Quote:
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).

--
Marcus Kwok
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.