[ create a new paste ] login | about

Link: http://codepad.org/HR5TwAG6    [ raw code | output | fork ]

C++, pasted on Nov 8:
#include <iostream>
#include <algorithm>

using std::cout;
using std::endl;

class Base
{
public:
	Base(int para)
	{
		a = para;
		cout << "Base::a = " << a << endl;
	}
private:
	int a;
};

class Derive : public Base
{
public:
	int foo()
	{
		cout << "Derived::foo() return 0" << endl;
		return 0;
	}
	Derive() : Base(foo()) {}
};

class Derive2 : public Base
{
public:
	int foo()
	{
		cout << "Derive2::foo() return " << b << endl;
		return b;
	}
	Derive2() :  Base(foo()),b(0) {}
private:
	int b;
};

int main(void)
{
	char buf[sizeof(Derive)+sizeof(Derive2)]; 
	std::fill(buf, buf + sizeof(buf), -1);
	Derive* cld = new(buf) Derive();
	Derive2* cld2 = new(buf + sizeof(Derive))Derive2();
	cld->~Derive();
	cld2->~Derive2();

	return 0;
}


Output:
1
2
3
4
Derived::foo() return 0
Base::a = 0
Derive2::foo() return -1
Base::a = -1


Create a new paste based on this one


Comments: