Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Templates and descendant classes

Reply
Thread Tools

Templates and descendant classes

 
 
Ales DOLECEK
Guest
Posts: n/a
 
      07-22-2003
Hello,

few days ago I asked how to grant firend status to template. Thanks to
Rob Williscroft for his help. Now I have run into problems while using
class derived from the class created by the template. I'll try to give
part of code leaving out unnecesarry things.

---- base ----

template <typename T> class ptr_t;

class base_t {
template <typename T> class ptr_t;
int _ref_count; // how many "smart poiters" reference this instance
public:
base_t(void) : _ref_count(0) {};
}

template <typename T>
class ptr_t {
T *_target;
public:
ptr_t(void) : _target(NULL) {};
ptr_t(T *t);
ptr_t(ptr_t &other);
}

---- derived ----

class request_t: public base_t
{
// added members
}

class request_p: public ptr_t<request_t>
{
public:
bool operator==(const int) const;
bool operator==(const string &) const;
}

---- problem ----

When I use the following code:

request_p request(new request_t(number)); // <-- line ics.cc:146

it seems that the "ptr_t(T *t)" constructor is not inheried in
request_p.

ics.cc:146: no matching function for call to `request_p::request_p
(request_t *)'
request.hh:35: candidates are: request_p::request_p(const request_p &)
request.hh:35: request_p::request_p()

I think that none of the ptr_t constructors got inherited into request_p
as at request.hh:35 is only closing brace of request_p declaration and
the candidates are probably just default constructors suplied by
compiler.

What do I do wrong? How should the declaration fo request_p look like?

Thanks in advance Ales
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-22-2003
"Ales DOLECEK" <(E-Mail Removed)> wrote...
> few days ago I asked how to grant firend status to template. Thanks to
> Rob Williscroft for his help. Now I have run into problems while using
> class derived from the class created by the template. I'll try to give
> part of code leaving out unnecesarry things.
>
> ---- base ----
>
> template <typename T> class ptr_t;
>
> class base_t {
> template <typename T> class ptr_t;
> int _ref_count; // how many "smart poiters" reference this instance
> public:
> base_t(void) : _ref_count(0) {};
> }
>
> template <typename T>
> class ptr_t {
> T *_target;
> public:
> ptr_t(void) : _target(NULL) {};
> ptr_t(T *t);
> ptr_t(ptr_t &other);
> }
>
> ---- derived ----
>
> class request_t: public base_t
> {
> // added members
> }
>
> class request_p: public ptr_t<request_t>
> {
> public:
> bool operator==(const int) const;
> bool operator==(const string &) const;
> }
>
> ---- problem ----
>
> When I use the following code:
>
> request_p request(new request_t(number)); // <-- line ics.cc:146
>
> it seems that the "ptr_t(T *t)" constructor is not inheried in
> request_p.


Correct. Constructors are NEVER inherited.

> ics.cc:146: no matching function for call to `request_p::request_p
> (request_t *)'
> request.hh:35: candidates are: request_p::request_p(const request_p &)
> request.hh:35: request_p::request_p()
>
> I think that none of the ptr_t constructors got inherited into request_p
> as at request.hh:35 is only closing brace of request_p declaration and
> the candidates are probably just default constructors suplied by
> compiler.
>
> What do I do wrong? How should the declaration fo request_p look like?


If you need to have a parameterised constructor in your derived
class, you have to define it yourself:

class request_p : public ptr_t<request_t>
{
...
request_p(request_t* t) : ptr_t<request_t>(t) {}
};

Victor


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-22-2003
Ales DOLECEK wrote in news:(E-Mail Removed):

> When I use the following code:
>
> request_p request(new request_t(number)); // <-- line ics.cc:146
>
> it seems that the "ptr_t(T *t)" constructor is not inheried in
> request_p.
>
> ics.cc:146: no matching function for call to `request_p::request_p
> (request_t *)'
> request.hh:35: candidates are: request_p::request_p(const request_p &)
> request.hh:35: request_p::request_p()
>
> I think that none of the ptr_t constructors got inherited into request_p
> as at request.hh:35 is only closing brace of request_p declaration and
> the candidates are probably just default constructors suplied by
> compiler.
>
>


Constructor's aren't inherited, the two that were found are just
the ones created by the compiler.

class request_p: public ptr_t<request_t>
{
public:
bool operator==(const int) const;
bool operator==(const string &) const;

request_p(request_t *p) : ptr_t<request_t>(p) {}
};

Note that if you're inheriting just to add op == you could just
add the following function's:

bool operator == ( ptr_t<request_t> const &lhs, int rhs )
{
return false /* || whatever */;
}

bool operator == ( ptr_t<request_t> const &lhs, string const &rhs )
{
return false /* || whatever */;
}

You can then add the other two the same way:

bool operator == ( int lhs, ptr_t<request_t> const &rhs )
{
return false /* || whatever */;
}

bool operator == ( string const &lhs, ptr_t<request_t> const &rhs )
{
return false /* || whatever */;
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const data in descendant classes Paul Smitton C++ 5 08-21-2007 07:04 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Access descendant class's module namespace from superclass Reid Priedhorsky Python 2 07-12-2005 04:00 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
css - descendant selectors Nik Coughin HTML 2 01-29-2004 12:12 AM



Advertisments