Re: Implementing member object's virtual functions
On Apr 9, 7:36 am, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
wrote:
Is it possible to implement member object's virtual functions, in the
containing class? If not, is it possible to simulate this behavior?
ex:
class test
{
protected:
virtual void fun() = 0;
};
class test1
{
protected:
void fun() {}
private:
test t1;
};
No, if the language allowed that then coupling would become a
nightmare.
Isn't class test meant to be abstract anyways?
Templates are not required but consider the implications:
#include <iostream>
class abstract
{
protected:
virtual void fun() = 0;
};
class concrete : public abstract
{
public:
void fun() { std::cout << "concrete::fun()\n"; }
};
class another : public concrete
{
public:
void fun() { std::cout << "another::fun()\n"; }
};
// type M must implement void fun()
template< typename M >
class test
{
M m;
public:
void fun() { m.fun(); }
};
int main()
{
test< concrete > t;
t.fun();
test< another > a;
a.fun();
}
/*
concrete::fun()
another::fun()
*/