* Syntax of constructor
@ 2006-03-20 3:11 Shriramana Sharma
2006-03-20 3:34 ` Uday Karan
0 siblings, 1 reply; 4+ messages in thread
From: Shriramana Sharma @ 2006-03-20 3:11 UTC (permalink / raw)
To: Linux C Programming List
A header file contains the following class declaration:
class CAA2DCoordinate
{
public:
//Constructors / Destructors
CAA2DCoordinate(): X(0), Y(0) {};
//member variables
double X;
double Y;
};
Now what I do not understand is the line:
CAA2DCoordinate(): X(0), Y(0) {};
I would have thought it would be:
CAA2DCoordinate() { X = 0; Y = 0; }
just like the QDate constructor:
QDate() { jd = 0; }
What is the meaning of the colon ":" here, and what are the (0) things, and
why are X and Y *outside* the braces?
Thanks for your patience.
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-20 W12-1 UTC+0530
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: Syntax of constructor
2006-03-20 3:11 Syntax of constructor Shriramana Sharma
@ 2006-03-20 3:34 ` Uday Karan
2006-03-20 8:17 ` Shriramana Sharma
0 siblings, 1 reply; 4+ messages in thread
From: Uday Karan @ 2006-03-20 3:34 UTC (permalink / raw)
To: Shriramana Sharma, linux-c-programming
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.
Here is a link that explains this particular topic:
http://www.blueturnip.com/projects/edu/cs/cpp/initializer-lists.html
-Uday
On 3/20/06, Shriramana Sharma <samjnaa@gmail.com> wrote:
> A header file contains the following class declaration:
>
> class CAA2DCoordinate
> {
> public:
> //Constructors / Destructors
> CAA2DCoordinate(): X(0), Y(0) {};
>
> //member variables
> double X;
> double Y;
> };
>
> Now what I do not understand is the line:
>
> CAA2DCoordinate(): X(0), Y(0) {};
>
> I would have thought it would be:
>
> CAA2DCoordinate() { X = 0; Y = 0; }
>
> just like the QDate constructor:
>
> QDate() { jd = 0; }
>
> What is the meaning of the colon ":" here, and what are the (0) things, and
> why are X and Y *outside* the braces?
>
> Thanks for your patience.
>
> --
>
> Tux #395953 resides at http://samvit.org
> playing with KDE 3.51 on SUSE Linux 10.0
> $ date [] CCE +2006-03-20 W12-1 UTC+0530
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: Syntax of constructor
2006-03-20 3:34 ` Uday Karan
@ 2006-03-20 8:17 ` Shriramana Sharma
2006-03-20 13:09 ` Glynn Clements
0 siblings, 1 reply; 4+ messages in thread
From: Shriramana Sharma @ 2006-03-20 8:17 UTC (permalink / raw)
To: Linux C Programming List
Monday, 20 March 2006 09:04 samaye tvayaa likhitam:
> 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...
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-20 W12-1 UTC+0530
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: Syntax of constructor
2006-03-20 8:17 ` Shriramana Sharma
@ 2006-03-20 13:09 ` Glynn Clements
0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2006-03-20 13:09 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
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@gclements.plus.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-03-20 13:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-20 3:11 Syntax of constructor Shriramana Sharma
2006-03-20 3:34 ` Uday Karan
2006-03-20 8:17 ` Shriramana Sharma
2006-03-20 13:09 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).