All pastes #824439 Raw Edit

Unbenannt

public text v1 · immutable
·published 2007-12-19 23:43 UTC
rendered paste body
#include <iostream>

using namespace std;

class A {
public:
	A(const string & b = "bar from A") : bar(b) {}
	virtual void foo() {
		cout << "this is A::foo() and bar is " << bar << endl;
	}
protected:
	string bar;
};

class B : public A {
public:
	B() : A("bar from B") {} 
	void foo() {
		cout << "this is B:foo() and bar is " << bar << endl;
	}
};

int
main(int argc, char ** argv)
{
	A myA;
	myA.foo();
	B myB;
	myB.foo();
	A otherA = myB;
	otherA.foo();
	// dynamic_cast crashes, triggers a warning "conversion can never succeed"
	A * tmpA = static_cast<B*>(&otherA);
	otherA = *tmpA;
	otherA.foo();
	return 0;
}