Templates and descendant classes

A

Ales DOLECEK

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
 
V

Victor Bazarov

Ales DOLECEK said:
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
 
R

Rob Williscroft

Ales DOLECEK wrote in
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.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top