home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!agate!apple!kaleida.com!moon!gordie
- From: gordie@duts.ccc.amdahl.com (Gordie Freedman)
- Newsgroups: comp.lang.c++
- Subject: Do temps hurt performance of overloaded +
- Date: 16 Dec 1992 23:57:58 GMT
- Organization: Happy hacker bait and tackle shop
- Lines: 55
- Distribution: world
- Message-ID: <1gofq7INN8q@golden.kaleida.com>
- Reply-To: gordie@duts.ccc.amdahl.com
- NNTP-Posting-Host: moon.kaleida.com
-
- If I do the following to overload binary + for a class, will temporary
- objects be created? And if so, what can I do to prevent this, to get the best
- performance?
-
-
-
- #include <stdio.h>
-
- // Class definition
- class Complex
- {
- public:
- Complex (void) : real(0.0), imaginary(0.0) {}
- Complex (Complex& c) : real(c.real), imaginary(c.imaginary) {}
- Complex (float x, float y) : real(x), imaginary(y) {}
- friend Complex operator + (Complex& x, Complex& y);
- void print (void);
-
- private:
- float real;
- float imaginary;
- };
-
- // Print routine
- void Complex::print (void)
- {
- fprintf (stdout, "Real %g, imaginary %g\n", real, imaginary);
- }
-
- // Overloaded + operator
- inline Complex operator + (Complex& x, Complex& y)
- {
- return Complex (x.real + y.real, x.imaginary + y.imaginary);
- }
-
- // Main test routine
- int
- main (int argc, char** argv)
- {
- Complex a(1, 2), b(10,20), c;
- Complex* dp;
-
- c = a + b;
- dp = new Complex (c + b);
-
- c.print();
- dp->print();
- return 0;
- }
-
-
- ---
- Gordie Freedman: gordie@kaleida.com Sigs are for kids
-
-
-