Overloading Prefix and Postfix Operators
Feb 10, 2014 at 1:18am UTC
Hello!
I've been working through some examples on how to overload operators, and I managed to create a program with a simple class, circle, that has overloaded prefix, postfix, and addition operators. only thing is that it has an unexpected output.
Here is the class definition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class SimpleCircle {
public :
SimpleCircle();
SimpleCircle(const int radius);
~SimpleCircle();
int getRadius() const {return *itsRadius;}
void setRadius(int newRadius) {*itsRadius = newRadius;}
const SimpleCircle & operator ++ ();
const SimpleCircle operator ++ (int x);
SimpleCircle operator +(const SimpleCircle & prm);
private :
int *itsRadius;
};
I think the problem is here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const SimpleCircle & SimpleCircle::operator ++() {
cout << "Prefix!\n" ;
++*itsRadius;
return *this ;
}
const SimpleCircle SimpleCircle::operator ++(int x) {
SimpleCircle temp(*this );
cout << "Postfix!\n" ;
++*itsRadius;
return temp;
}
This is my main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
int main() {
cout << "Hello world!" << endl;
SimpleCircle circle(9);
SimpleCircle sndCircle;
circle++;
sndCircle++;
++circle;
++circle;
cout << circle.getRadius() << "\n" ;
cout << sndCircle.getRadius() << "\n" ;
cout << "Success!" << endl;
return 0;
}
I expected an output where SimpleCircle circle's radius was 11;
and sndCircle's 7 (my default constructor initializes itsRadius to 5). Instead, I got
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
Hello world!
0Constructor
Constructor!
Postfix!
Destructor!
Postfix!
Destructor!
Prefix!
Prefix!
1
18550785
Success!
*** Error in `./advancedclasses': double free or corruption (fasttop): 0x00000000011b1030 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7aa16)[0x7fdc11533a16]
/lib/x86_64-linux-gnu/libc.so.6(+0x7b793)[0x7fdc11534793]
./advancedclasses[0x400b0b]
./advancedclasses[0x400d2c]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fdc114da995]
./advancedclasses[0x400949]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:01 4063859 /home/ayush/Ayush/ExCodes/advancedclasses
00601000-00602000 rw-p 00001000 08:01 4063859 /home/ayush/Ayush/ExCodes/advancedclasses
011b1000-011d2000 rw-p 00000000 00:00 0 [heap]
7fdc114b9000-7fdc1165c000 r-xp 00000000 08:01 4456717 /lib/x86_64-linux-gnu/libc-2.17.so
7fdc1165c000-7fdc1185b000 ---p 001a3000 08:01 4456717 /lib/x86_64-linux-gnu/libc-2.17.so
7fdc1185b000-7fdc1185f000 r--p 001a2000 08:01 4456717 /lib/x86_64-linux-gnu/libc-2.17.so
7fdc1185f000-7fdc11861000 rw-p 001a6000 08:01 4456717 /lib/x86_64-linux-gnu/libc-2.17.so
7fdc11861000-7fdc11865000 rw-p 00000000 00:00 0
7fdc11865000-7fdc1187a000 r-xp 00000000 08:01 4456451 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdc1187a000-7fdc11a7a000 ---p 00015000 08:01 4456451 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdc11a7a000-7fdc11a7b000 rw-p 00015000 08:01 4456451 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdc11a7b000-7fdc11b77000 r-xp 00000000 08:01 4456723 /lib/x86_64-linux-gnu/libm-2.17.so
7fdc11b77000-7fdc11d77000 ---p 000fc000 08:01 4456723 /lib/x86_64-linux-gnu/libm-2.17.so
7fdc11d77000-7fdc11d78000 r--p 000fc000 08:01 4456723 /lib/x86_64-linux-gnu/libm-2.17.so
7fdc11d78000-7fdc11d79000 rw-p 000fd000 08:01 4456723 /lib/x86_64-linux-gnu/libm-2.17.so
7fdc11d79000-7fdc11e5e000 r-xp 00000000 08:01 263227 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7fdc11e5e000-7fdc1205d000 ---p 000e5000 08:01 263227 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7fdc1205d000-7fdc12065000 r--p 000e4000 08:01 263227 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7fdc12065000-7fdc12067000 rw-p 000ec000 08:01 263227 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7fdc12067000-7fdc1207c000 rw-p 00000000 00:00 0
7fdc1207c000-7fdc1209d000 r-xp 00000000 08:01 4456708 /lib/x86_64-linux-gnu/ld-2.17.so
7fdc12271000-7fdc12276000 rw-p 00000000 00:00 0
7fdc12299000-7fdc1229d000 rw-p 00000000 00:00 0
7fdc1229d000-7fdc1229e000 r--p 00021000 08:01 4456708 /lib/x86_64-linux-gnu/ld-2.17.so
7fdc1229e000-7fdc122a0000 rw-p 00022000 08:01 4456708 /lib/x86_64-linux-gnu/ld-2.17.so
7fffaef3a000-7fffaef5b000 rw-p 00000000 00:00 0 [stack]
7fffaeffe000-7fffaf000000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
------------------
(program exited with code: 134)
Press return to continue
What did I do wrong? Any suggestions would be appreciated.
Feb 10, 2014 at 1:26am UTC
Why does
itsRadius have to be a
int *
?
Why not just an
int
?
Your problem is most likely forgetting the "Rule of 3":
http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three
In other words, when you do this:
1 2 3 4
SimpleCircle temp(*this );
cout << "Postfix!\n" ;
++*itsRadius;
return temp;
This copies the pointer, so
this->itsRadius and
temp.itsRadius are now pointing at the same thing.
Then
temp gets destroyed, and along with it goes the
itsRadius of the original object as well.
tl;dr: Define a copy constructor. Better yet, change
itsRadius to an
int
and forget the pointers.
Feb 10, 2014 at 12:37pm UTC
thanks for your reply! the reason I have to use int * is for a programming exercise. I would have rather used an int, but I had to use a pointer. I guess I needed to, because know I know I need the practice. Thanks for your reply!
Topic archived. No new replies allowed.