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