473,386 Members | 1,621 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,386 software developers and data experts.

PLEASE HELP : Template inheritance problem gcc 3.2.3

Could some C++ guru please help me. I have a template based general linked list
class that I want to inherit publicly to create a queue class. In the case of
non-template inheritance, I can use:
class A{ };
class B: public A{};

And for the constructor of B:
B:B():A(){}

now suppose I have a template as:
template <class T>
class A{ ... };

template <class T>
class B: public A<T>{ ... }

What is the syntax for the constructor of B in this case, i.e., what goes into
the following - where the question marks are?

template <class T>
B<T>::B():???????{}

Thanks in advance for your help!!
Jul 22 '05 #1
3 2968
"Gandu" <cp**********@yahoo.com> wrote:
now suppose I have a template as:
template <class T>
class A{ ... };

template <class T>
class B: public A<T>{ ... }

What is the syntax for the constructor of B in this case, i.e., what goes into the following - where the question marks are?

template <class T>
B<T>::B():???????{}
Why not try the obvious:

template <class T>
class B: public A<T>{
B() : A<T>( [ argument list] ) { }
};

?

Sometimes if the base class is specified using a complex expression you need to
use a typedef.
Thanks in advance for your help!!


Jonathan
Jul 22 '05 #2
It depends on your defined constructors.

You can legally do this:
template <class T>
class A{ public: A(int aVal): m_aVal(aVal) {} private: int m_aVal; };

template <class T>
class B: public A<T>{ ... }
template <class T>
B<T>::B():A(1) {...}
but equally you can do:

template <class T>
class B : public A<T>
public:
B::B() {} // which will call the default constructor of an A<T> class.
but if you have no need to use anything but default constructors don't feel
compelled to do so.

"Gandu" <cp**********@yahoo.com> wrote in message
news:3c*************************@posting.google.co m... Could some C++ guru please help me. I have a template based general linked
list
class that I want to inherit publicly to create a queue class. In the
case of
non-template inheritance, I can use:
class A{ };
class B: public A{};

And for the constructor of B:
B:B():A(){}

now suppose I have a template as:
template <class T>
class A{ ... };

template <class T>
class B: public A<T>{ ... }

What is the syntax for the constructor of B in this case, i.e., what goes
into
the following - where the question marks are?

template <class T>
B<T>::B():???????{}

Thanks in advance for your help!!


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 22 '05 #3
Gandu wrote:
Could some C++ guru please help me. I have a template based general linked list
class that I want to inherit publicly to create a queue class. In the case of
non-template inheritance, I can use:
class A{ };
class B: public A{};

And for the constructor of B:
B:B():A(){}

now suppose I have a template as:
template <class T>
class A{ ... };

template <class T>
class B: public A<T>{ ... }

What is the syntax for the constructor of B in this case, i.e., what goes into
the following - where the question marks are?

template <class T>
B<T>::B():???????{}

Thanks in advance for your help!!


What Jonathan said (move the constructor definition inside the class
definition, which has the side effect of making the constructor inline),
or ...

template <typename T>
struct A
{
A (/* parameters for A */);
};

template <typename T>
struct B : A <T>
{
B (/* parameters for B */);
};

template <typename T>
A <T>::A (/* parameters for A */) { }

// ... like this:

template <typename T>
B <T>::B (/* parameters for B */) : A <T> (/* arguments for A */) { }

void test_syntax ()
{
B <int> b (/* arguments for B */);
}

--
Regards,
Buster
Jul 22 '05 #4

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

Similar topics

3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.