AM> What's wrong with this code? I guess the temporary object created in
AM> operator+ member is lost somewhere, but I don't know why, and I don'
AM> know how to make it work :-(
TM>What doesn't work? What's the output of the main() function? I don't
TM>see any "fatal" problems in your code. But allow me some remarks:
It didn't work because I run it in M$C, after I made some mistakes and
put some dirty stuff in the memory. I guess the new was returning 0.
AM> class Vector
AM> {
AM> public:
AM> double x, y;
TM>Don't make these public. Better declare them private and provide
TM>inline accessor methods:
TM>private:
TM> double x, y;
TM>public:
TM> double X() const { return x; }
TM> double Y() const { return y; }
What is the advantage of this? I mean, besides aesthetical
considerations :-)
AM> Vector(double xx = 0, double yy = 0); // constructor
TM>This constructor may cause you problems (not in the main() function
TM>you give below, though) like every constructor taking one argument: it
TM>can be used by the compiler for implicit conversions.
??? How can it give problems?
AM> Vector::Vector(double xx, double yy)
AM> {
AM> x = xx;
AM> y = yy;
AM> }
AM> Vector::Vector(const Vector & V2)
AM> {
AM> x = V2.x;
AM> y = V2.y;
AM> }
TM>Initialize the members in the member-initializer-list rather than in
TM>the constructor's body:
TM>Vector::Vector(double xx, double yy)
TM> : x(xx),
TM> y(yy)
TM>{
TM>}
TM>Vector::Vector(const Vector & V2)
TM> : x(V2.x),
TM> y(V2.y)
TM>{
TM>}
Ok. But as you must have imagined, I wish to generalize it to a
n-dimensional Vector. Then I guess I can't initialize them this way
Alberto Monteiro
---
þ SLMR 2.1a þ Bred to kill, not to care/do just as we say (Metallica)
--- FMail/386 1.02
---------------
* Origin: CentroIn! +55-21-205-0281, 41 lines, 24h, RJ/Brazil (4:802/21)
|