Skip to content

Instantly share code, notes, and snippets.

@zwily
Created February 5, 2009 20:17
Show Gist options
  • Save zwily/58974 to your computer and use it in GitHub Desktop.
Save zwily/58974 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class A {
public:
A(int val) : m_val(val) {}
A& operator=(A& a) {
m_val += a.m_val;
return *this;
}
int val() { return m_val; }
protected:
int m_val;
};
class B : public A {
public:
B(int val) : A(val) {}
B& operator=(B& b) {
A::operator=(b);
m_val += 5;
return *this;
}
};
int main(int argc, char** argv) {
A a1(1);
A a2(2);
a1 = a2;
cout << "should be 3: ";
cout << a1.val() << endl;
B b1(1);
B b2(2);
b1 = b2;
cout << "should be 8: ";
cout << b1.val() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment