PLEASE HELP : Template inheritance problem gcc 3.2.3

G

Gandu

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!!
 
J

Jonathan Turkanis

Gandu said:
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
 
A

adbarnet

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 said:
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.
 
B

Buster

Gandu said:
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 */);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top