473,748 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates and inheritance

Hi,

I have the following problem. Consider two simple classes AA and BB,
BB inherits from AA. AA contains a class A inside and a pure virtual
method getA returning an object of class A. BB overrides both.
Here is the code:

class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};

class BB : public AA {
public:
class A : public AA::A {
public:
A(void) {};
};
virtual AA::A getA(void) { return BB::A(); };
virtual ~BB() {};
};

BB b;

int main(void)
{
return 0;
}

So far, so good. Everything goes fine. But now suppose that we
change the classes AA/BB into class *templates*:

template <class T>
class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};

template <class T, unsigned N>
class BB : public AA<T{
public:
class A : public AA<T>::A {
public:
A(void) {};
};
virtual AA<T>::A getA(void) { return BB::A(); };
virtual ~BB() {};
};

BB<int,5b;

int main()
{
return 0;
}

Although I didn't do any other changes, I cannot compile it
(with GNU C++ 4.1.0). I receive the following errors:

wzlsd2.cpp:19: error: type 'AA<T>' is not derived from type 'BB<T, N>'
wzlsd2.cpp:19: error: expected ';' before 'getA'
wzlsd2.cpp:23: error: cannot declare variable 'b' to be of abstract type
'BB<int, 5u>'
wzlsd2.cpp:13: note: because the following virtual functions are pure within
'BB<int, 5u>':
wzlsd2.cpp:8: note: AA<T>::A AA<T>::getA() [with T = int]

Obviously the first error is the important one. Compiler does not accept
the type AA<T>::A, althougth it accepted it just few lines above (when
declaring class BB<T,N>::A).

And my question is: what's wrong? What mistake I'm doing here?
Or is this a compiler's bug?

TIA,
Przemek


Mar 30 '07 #1
4 3984
Przemyslaw Koprowski wrote:
Hi,

I have the following problem. Consider two simple classes AA and BB,
BB inherits from AA. AA contains a class A inside and a pure virtual
method getA returning an object of class A. BB overrides both.
Here is the code:

class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};

class BB : public AA {
public:
class A : public AA::A {
public:
A(void) {};
};
virtual AA::A getA(void) { return BB::A(); };
virtual ~BB() {};
};

BB b;

int main(void)
{
return 0;
}

So far, so good. Everything goes fine. But now suppose that we
change the classes AA/BB into class *templates*:

template <class T>
class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};

template <class T, unsigned N>
class BB : public AA<T{
public:
class A : public AA<T>::A {
public:
A(void) {};
};
virtual AA<T>::A getA(void) { return BB::A(); };
virtual ~BB() {};
};

BB<int,5b;

int main()
{
return 0;
}

Although I didn't do any other changes, I cannot compile it
(with GNU C++ 4.1.0). I receive the following errors:

wzlsd2.cpp:19: error: type 'AA<T>' is not derived from type 'BB<T, N>'
wzlsd2.cpp:19: error: expected ';' before 'getA'
wzlsd2.cpp:23: error: cannot declare variable 'b' to be of abstract type
'BB<int, 5u>'
wzlsd2.cpp:13: note: because the following virtual functions are pure within
'BB<int, 5u>':
wzlsd2.cpp:8: note: AA<T>::A AA<T>::getA() [with T = int]

Obviously the first error is the important one. Compiler does not accept
the type AA<T>::A, althougth it accepted it just few lines above (when
declaring class BB<T,N>::A).
virtual typename AA<T>::A getA().

And remove the BB:: inside getA

virtual typename AA<T>::A getA() { return A(); }
Mar 30 '07 #2
On Mar 30, 12:49 pm, Przemyslaw Koprowski <o...@siggraph. pkoprowski>
wrote:
So far, so good. Everything goes fine. But now suppose that we
change the classes AA/BB into class *templates*:

template <class T>
class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};

};

template <class T, unsigned N>
class BB : public AA<T{
public:
class A : public AA<T>::A {
public:
A(void) {};
};
virtual AA<T>::A getA(void) { return BB::A(); };
virtual ~BB() {};

};

BB<int,5b;

int main()
{
return 0;

}

Although I didn't do any other changes, I cannot compile it
(with GNU C++ 4.1.0). I receive the following errors:

And my question is: what's wrong? What mistake I'm doing here?
Or is this a compiler's bug?
For what it's worth, this compiles and runs on Visual Studio. That
doesn't necessarily mean it's a compiler bug in g++, it could be an
extension or a bug in VS.

Michael

Mar 30 '07 #3
On Fri, 30 Mar 2007 18:52:42 +0000, red floyd wrote:
virtual typename AA<T>::A getA().

And remove the BB:: inside getA

virtual typename AA<T>::A getA() { return A(); }
Thanks. It works, now.

Przemek


Mar 30 '07 #4
Przemyslaw Koprowski wrote:
On Fri, 30 Mar 2007 18:52:42 +0000, red floyd wrote:
>virtual typename AA<T>::A getA().

And remove the BB:: inside getA

virtual typename AA<T>::A getA() { return A(); }
Thanks. It works, now.

Przemek
By the way, I believe g++ complaining about BB:A() is an error in g++.
Comeau likes

virtual typename AA<T>::A getA() { return BB::A(); }
Mar 30 '07 #5

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

Similar topics

1
1640
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 moment) parser.setSwitch(BoolSwitch("v", "verbose", &bVerbose)); parser.setSwitch(GeometrySwitch("s", "size", &sizeGeometry));
3
2542
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
16
16285
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
10
9631
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 instead of inheritance, and other way around? thoughts, please.
11
1823
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
4
1496
by: qning88 | last post by:
I would like to find out how I can effectively make use of templates in theexample below. In Class A, I have 3 overloaded member functions "send" for handling the different messages. Although the messages are different, the way to handle the message is the same. Hence I'm thinking of using templates to reduce the number of lines in the code. ------ ClassA.h ------ struct Message1;
6
2109
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 discussions filled with a lot of "noise" which is very distracting, or literature that doesn't specifically "compare" the two. I do have access to resources that address the two topics individually, and I'm wondering if you have any recommendations on...
3
2181
by: Paulo Matos | last post by:
Hi all, I'm curious if I can say in C++ that a certain template can only be instantiated (template-wise) by a class implementing a certain interface (inheriting from a given abstract class). Regards, Paulo Matos
5
1892
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 example********** template<typename Tclass TypedInterface { public: virtual void TestFunction( T * ) = 0;
2
1114
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 something like:
0
8983
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9528
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8235
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.