TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALL
from: ALBERTO MONTEIRO
date: 1997-12-12 13:48:00
subject: What`s wrong with this?

What's wrong with this code? I guess the temporary object created in the
operator+ member is lost somewhere, but I don't know why, and I don't
know how to make it work :-(
#include 
#include 
#include 
#include 
#if !defined(max)
#define max(a, b)   ((a) > (b) ? (a) : (b)) // ridiculous...
#endif
class Vector
{
public:
  double x, y;
  Vector(double xx = 0, double yy = 0);    // constructor
  Vector(const Vector & V2);             // copy constructor
  ~Vector();                             // destructor
  Vector   operator+(const Vector & V2) const;
  Vector & operator=(const Vector & V2);
};
Vector::Vector(double xx, double yy)
{
  x = xx;
  y = yy;
}
Vector::Vector(const Vector & V2)
{
  x = V2.x;
  y = V2.y;
}
Vector::~Vector()
{
}
Vector Vector::operator+(const Vector & V2) const
{
  Vector add;
  add.x = x + V2.x;
  add.y = y + V2.y;
  return add;
}
Vector & Vector::operator=(const Vector & V2)
{
  if (this != &V2) {
    x = V2.x;
    y = V2.y;
  }
  return *this;
}
int main(void)
{
  Vector V1(1, 2), V2(3, 4), V3;
  V3 = V1 + V2;
  cout << V3.x << "," << V3.y;
  return 1;
}
---
 þ SLMR 2.1a þ Blood feeds the War Machine as it eats its way across the
--- FMail/386 1.02
---------------
* Origin: CentroIn! +55-21-205-0281, 41 lines, 24h, RJ/Brazil (4:802/21)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.