AM> TM> double X() const { return x; }
AM> TM> double Y() const { return y; }
AM> What is the advantage of this? I mean, besides aesthetical
AM> considerations :-)
That the class can prevent the members from being overwritten from
outside.
AM> AM> Vector(double xx = 0, double yy = 0); // constructor
AM> TM>This constructor may cause you problems (not in the main() fun
AM> TM>you give below, though) like every constructor taking one argumen
AM> TM>can be used by the compiler for implicit conversions.
AM> ??? How can it give problems?
For example if you change a function parameter from a C-style array to
your Vector class (extended to more dimensions):
The first version might have looked like this:
void f(double *vector) {...}
which can be called like this:
f(0);
If you now change f to
void f(Vector &v) {...}
this call still compiles, because 0 is now silently converted into a
Vector.
Declaring the constructor explicit would prevent the compiler from
performing this silent conversion:
explicit Vector(double xx = 0, double yy = 0); // constructor
AM> TM>Initialize the members in the member-initializer-list rather th
AM> TM>the constructor's body:
[...]
AM> Ok. But as you must have imagined, I wish to generalize it to a
AM> n-dimensional Vector. Then I guess I can't initialize them this way
No, I didn't imagine that (although I admit that I might have...).
Yes, you are right. But then I'd suggest that you use the standard
vector template.
Thomas
---
þ MM 1.0 #0113 þ Pit crew guys do it in 7 seconds.
--- PCBoard (R) v15.22/M 25
---------------
* Origin: McMeier & Son BBS (2:301/138)
|