 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
widmont Guest
|
Posted: Tue Feb 28, 2006 12:06 am Post subject: variable declaration |
|
|
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
|
Posted: Tue Feb 28, 2006 1:06 am Post subject: Re: variable declaration |
|
|
| Quote: | template<class T
class DefaultInitialised : public T
{
DefaultInitialised() : T()
|
{
}
| Quote: | };
int main()
{
DefaultInitialised<ostringstream> k;
} |
|
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Tue Feb 28, 2006 1:06 am Post subject: Re: variable declaration |
|
|
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
|
Posted: Tue Feb 28, 2006 2:06 am Post subject: Re: variable declaration |
|
|
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
|
Posted: Tue Feb 28, 2006 3:06 pm Post subject: Re: variable declaration |
|
|
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
|
Posted: Thu Mar 02, 2006 7:23 pm Post subject: Re: variable declaration |
|
|
"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 |
|
 |
|
|
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
|
|