Inheritance and templates

S

Simon Elliott

A polymorphic class which has a bunch of descendents created via
templae class and a convenience template function which returns an
auto_ptr to the class:

#include <iostream>
#include <vector>
#include <memory>

class fooBase
{
public:
virtual void DoSomething(void) = 0;
virtual ~fooBase(void){}
};

typedef std::auto_ptr<fooBase> fooBasePtr;

template<typename T> class fooDerived:public fooBase
{
private:
T* valuePtr_;
public:
fooDerived(T& value):valuePtr_(&value){}
virtual void DoSomething(void)
{
std::cout << *valuePtr_ << std::endl;
}
};

template<typename T> fooBasePtr MakeFoo(T& value)
{
return(fooBasePtr(new fooDerived<T>(value)));
// why can't I do: return(fooBasePtr(new fooDerived(value)));
}

int main()
{
std::string test = "Hello!!";
fooBasePtr myFooBase = MakeFoo(test);
myFooBase->DoSomething();
}

So far, so good. Now I decide to make my class handle a variety of
containers as well. For various good reasons these are all implemented
as:

class Something:public std::some_container<some_type>{};

#include <iostream>
#include <vector>
#include <memory>

class fooBase
{
public:
virtual void DoSomething(void) = 0;
virtual ~fooBase(void){}
};

typedef std::auto_ptr<fooBase> fooBasePtr;

template<typename T> class fooDerived:public fooBase
{
private:
T* valuePtr_;
public:
fooDerived(T& value):valuePtr_(&value){}
virtual void DoSomething(void)
{
std::cout << *valuePtr_ << std::endl;
}
};

template<typename T> fooBasePtr MakeFoo(T& value)
{
return(fooBasePtr(new fooDerived<T>(value)));
// why can't I do: return(fooBasePtr(new fooDerived(value)));
}

class Container1:public std::vector<int>{};
class Container2:public std::vector<std::string>{};

template<typename T> class fooDerivedContainer:public fooBase
{
private:
T* valuePtr_;
public:
fooDerivedContainer(T& value):valuePtr_(&value){}
virtual void DoSomething(void)
{
for (T::const_iterator ip = valuePtr_->begin(); ip <
valuePtr_->end(); ++ip)
{
std::cout << *ip << std::endl;
}
}
};

// I can have a separate helper function which creates these
template<typename T> fooBasePtr MakeFooContainer(T& value)
{
return(fooBasePtr(new fooDerivedContainer<T>(value)));
}

// Or a specific helper function for each type which creates these
fooBasePtr MakeFoo(Container1& value)
{
return(fooBasePtr(new fooDerivedContainer<Container1>(value)));
}


int main()
{
std::string test = "Hello!!";
fooBasePtr myFooBase = MakeFoo(test);
myFooBase->DoSomething();

Container1 myContainer1;
myContainer1.push_back(1);
myContainer1.push_back(2);
myContainer1.push_back(3);

myFooBase = MakeFooContainer(myContainer1);
myFooBase->DoSomething();

myFooBase = MakeFoo(myContainer1);
myFooBase->DoSomething();
}

What I actually want to do here is to have a single overloaded helper
function MakeFoo(), but without having to create an instance of
MakeFoo() for each container class type. Do my container classes have
anything in common (or can they be given anything in common) which will
allow this to happen?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top