473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 19 '05 #1
2 2596
"Ales DOLECEK" <al****@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
Jul 19 '05 #2
Ales DOLECEK wrote in news:20****************************@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/
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
16
by: chameleon | last post by:
I have 2 classes with exactly the same members (all static except dtor/ctor). Classes have different implementantion in only one static member function and first class has one more member...
5
by: Paul Smitton | last post by:
Hello, I would like to be able to store some constant data that is specific to each descendant class. This data would then be accessable by base class functions. However, I cannot find out how...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.