By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

Inheritance and templates

P: n/a
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?

--
Simon Elliott http://www.ctsn.co.uk
Jul 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.