473,406 Members | 2,404 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 with templates

If I have the following:

class dataclass1
{
// ...
};

class dataclass2
{
// ...
};

class abstractbase1
{
public:
abstractbase1();
virtual ~abstractbase1();

virtual func1() = 0;
virtual func2() = 0;
};

template<class R> class templateclass1 : public abstractbase1
{
public:
templateclass1(typename R& data1) : m_data(data1) { }

virtual ~templateclass1() {}

protected:
typename R& m_data;
};

class derived2 : public templateclass1<dataclass1>
{
public:
derived2(dataclass1& data) :
templateclass1<dataclass1>(data)

virtual ~derived2() {}

virtual func1();
virtual func2();
};

class derived3 : public templateclass1<dataclass2>
{
public:
derived3(dataclass1& data) :
templateclass1<dataclass1>(data)

virtual ~derived3() {}

virtual func1();
virtual func2();
};

Now suppose I have 20 more classes like derived2 which instantiate
templateclass1 with dataclass1, and 20 more classes like derived3
which instantiate templateclass1 with dataclass2.

The question is: would the compiler typically instantiate
templateclass1<dataclass1> 21 times and templateclass1<dataclass2> 21
times (once for each derived class), resulting in large amounts of
duplicate code, or would the compiler only instantiate templateclass1
once for each unique parameter with which it is supplied? If
templateclass1<dataclass1> is instantiated 21 times, is there any way
to avoid this?

TIA,
sf
Jul 22 '05 #1
1 967
Steve Fischer wrote:

<snip>
The question is: would the compiler typically instantiate
templateclass1<dataclass1> 21 times and templateclass1<dataclass2> 21
times (once for each derived class), resulting in large amounts of
duplicate code,
No. In general, you'd have great difficulty using a template if every
reference to it with the same parameter type were instantiated to a
separate class.
or would the compiler only instantiate templateclass1
once for each unique parameter with which it is supplied?

<snip>

Yes.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #2

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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.