Can you please tell me why one method is better than the other ( code inside!)?
Hi,
Can someone please tell me what is the advantage of method one over method two?
Thanks
Shahram
Code:
#include <iostream>
#include "boost/shared_ptr.hpp"
//
// method 1
//
class base{
protected:
class inner{
public:
virtual int f()=0;
};
boost::shared_ptr<base::inner> ptr_inner;
base(boost::shared_ptr<base::inner> new_inner):ptr_inner(new_inner){;};
public:
int f(){return(ptr_inner->f());};
};
class drived:public base{
private:
class inner:public base::inner{
int f(){return(1);};
};
public:
drived(): base(boost::shared_ptr<base::inner>(new drived::inner)){};
};
//
// method 2
//
class base_prime{
public:
virtual int f()=0;
};
class drived_prime:public base_prime{
public:
int f(){return(1);};
};
int main(int argc, char *argv[])
{
base b = drived();
base_prime b_prime = drived_prime();
boost::shared_ptr<base_prime> b_prime(new drived_prime());
std::cout << b.f()<<std::endl;
std::cout << b_prime->f()<<std::endl;
return (0);
}