plane3( const point3& N, float D) : n( N ), d( D )
{
// CODE HERE.
}
It''s the ": n( N ), d( D )" part that bother me. I''ve been asking around but no one I know could tell.
Question about an odd function declaration
I''ve been reading a book that use this kind of declaration all over the place :
The stuff after the colon is used to initialize the class. Let me explain a bit further. As you know, classes have constructors, many of which take parameters to initialize the object. For consistency, C++ allows you to use a similar syntax for built-in types, too. Anyway, let's say you have a class with 2 members you have to initialize, one an int and one a char*. This is one way to initialize your class:
And here's the preferred method:
In a simplistic sense, they both do the same thing: they initialize members. There's another use for this stuff. If your class is derived from some other class and that parent class has a certain constructor that needs to be called, use the initialization syntax for that also. For example:
That's about it. One thing to keep in mind is that you only use the initialization list where the function is actually implemented. So, if you implement it in a source file, it goes there and not in the header. If it's implemented in the header and not a source file, it goes in the header. Oh...and, of course, you can only do this with constructors.
Hope that helps.
Edited by - merlin9x9 on March 30, 2001 4:49:48 PM
|
And here's the preferred method:
|
In a simplistic sense, they both do the same thing: they initialize members. There's another use for this stuff. If your class is derived from some other class and that parent class has a certain constructor that needs to be called, use the initialization syntax for that also. For example:
|
That's about it. One thing to keep in mind is that you only use the initialization list where the function is actually implemented. So, if you implement it in a source file, it goes there and not in the header. If it's implemented in the header and not a source file, it goes in the header. Oh...and, of course, you can only do this with constructors.
Hope that helps.
Edited by - merlin9x9 on March 30, 2001 4:49:48 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement