Re: Syntax of constructor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Shriramana Sharma wrote:

> > That's called the initializer list.  I suggest you get a book that
> > explains these things.  C++ Primer or The C++ Programming Language
> > would be good.
> 
> Thanks. I am reading/referring to a PDF copy of Thinking in C++ but of course 
> without knowing the name of this syntax "initializer list" I cannot search 
> through the PDF.
> 
> Now what I would like to know is: Is
> 
> > >         CAA2DCoordinate(): X(0), Y(0) {};
> 
> much different from:
> 
> > > CAA2DCoordinate() { X = 0; Y = 0; }
> 
> ? Seeing as X and Y are not const-s, I do not see the point in initializing 
> outside the (empty) braces. If they were const-s, TICP tells me that they 
> must be initialized before the *start* of the function so the X(0) syntax is 
> necessary. But here they are not const-s...

In which case, both forms are valid, and equivalent.

You also need to use an initialiser if you need to initialise the
member variable through a constructor.

Given that you have to use an initialiser in some cases, and you are
allowed to use it in the other cases (i.e. non-const primitive types),
you may as well *always* use initialisers to initialise member
variables, rather than a mix of initialisers and assignments.

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux