473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Inheritance and templates

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
0 912

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
5
by: Dart | last post by:
What are the major advantages of inheritance of a class of no virtual functions, promoting code re-use? Thanks!
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
6
by: Ravi Rao | last post by:
Hi, This is about "templates vs inheritance" (please, before you flame me, I do understand that they cover largely non-intersecting domains). Apart from D&E*, I've found either online...
4
by: Joe | last post by:
We are working on a project that requires 3rd parties (ie, consultants) to use our ASP.net webforms in a very reusable manner. The big catch is that we are not allowed to give them source. There...
4
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
2
by: Isaac Gelado | last post by:
Hi, I am having problems with inheritance in templates classes. Say I have the following classes: class A {}; class B: public A {}; template<typename Tclass C {}; Now in my code I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.