[ create a new paste ] login | about

Link: http://codepad.org/9FqUk0Fj    [ raw code | fork ]

C++, pasted on Apr 28:
class I {
public:
	virtual void Run() = 0;
};

template <class B>
class S : public I {
	B mb;
public:
	S(B b)
		:mb(b)
	{}

	virtual void Run()
	{
		mb.Run();
	}
};

class A {
	mutable I *mi;

public:
	A()
		:mi(0)
	{}

	template <class B>
	A(B b)
		:mi(new S<B>(b))
	{}

	A(const A &a)
		:mi(a.mi)
	{
		a.mi = 0;
	}

	template <class B>
	A &operator =(B b)
	{
		delete mi;
		mi = new S<B>(b);
		return *this;
	}

	A &operator =(const A &a)
	{
		delete mi;
		mi = a.mi;
		a.mi = 0;
		return *this;
	}

	I *operator ->()
	{
		assert(mi);
		return mi;
	}
};

struct instanceOfB {
	void Run()
	{}
};

int main(int argc, const char **argv)
{
	A a = instanceOfB(); // error: no matching function for call to "A::A(A)"
	//A a; a = instanceOfB(); // works
	instanceOfB i;
	//A a(i); // works
	a->Run();

	return 0;
}


Output:
No errors or program output.


Create a new paste based on this one


Comments: