Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Templates and descendant classes (http://www.velocityreviews.com/forums/t275932-templates-and-descendant-classes.html)

Ales DOLECEK 07-22-2003 05:02 PM

Templates and descendant classes
 
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

Victor Bazarov 07-22-2003 05:53 PM

Re: Templates and descendant classes
 
"Ales DOLECEK" <ales_d@seznam.cz> 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



Rob Williscroft 07-22-2003 06:42 PM

Re: Templates and descendant classes
 
Ales DOLECEK wrote in news:20030722190201.1db38d1b.ales_d@seznam.cz:

> 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/


All times are GMT. The time now is 07:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.