PLEASE HELP : Template inheritance problem gcc 3.2.3



 DEVELOP > c-Plus-Plus > PLEASE HELP : Template inheritance problem gcc 3.2.3

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER