| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Gandu" |
| Date: |
05 Dec 2004 04:47:06 PM |
| Object: |
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!!
.
|
|
| User: "Jonathan Turkanis" |
|
| Title: Re: PLEASE HELP : Template inheritance problem gcc 3.2.3 |
05 Dec 2004 04:59:51 PM |
|
|
"Gandu" <cpptutor2000@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
.
|
|
|
|
| User: "Buster" |
|
| Title: Re: PLEASE HELP : Template inheritance problem gcc 3.2.3 |
05 Dec 2004 08:35:17 PM |
|
|
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
.
|
|
|
|
| User: "adbarnet ad@dontmailmethx" |
|
| Title: Re: PLEASE HELP : Template inheritance problem gcc 3.2.3 |
05 Dec 2004 05:28:13 PM |
|
|
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" <cpptutor2000@yahoo.com> wrote in message
news:3c7c9804.0412051447.1751739@posting.google.com...
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
.
|
|
|
|

|
Related Articles |
|
|