Templates and descendant classes

Discussion in 'C++' started by Ales DOLECEK, Jul 22, 2003.

  1. Ales DOLECEK

    Ales DOLECEK Guest

    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
     
    Ales DOLECEK, Jul 22, 2003
    #1
    1. Advertising

  2. "Ales DOLECEK" <> 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
     
    Victor Bazarov, Jul 22, 2003
    #2
    1. Advertising

  3. Ales DOLECEK wrote in news::

    > 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/
     
    Rob Williscroft, Jul 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nik Coughin

    css - descendant selectors

    Nik Coughin, Jan 28, 2004, in forum: HTML
    Replies:
    2
    Views:
    392
    Alliss
    Jan 29, 2004
  2. JKop
    Replies:
    3
    Views:
    492
  3. Reid Priedhorsky
    Replies:
    2
    Views:
    337
    Bengt Richter
    Jul 12, 2005
  4. recover
    Replies:
    2
    Views:
    832
    recover
    Jul 25, 2006
  5. Paul Smitton

    const data in descendant classes

    Paul Smitton, Aug 20, 2007, in forum: C++
    Replies:
    5
    Views:
    347
    Paul Smitton
    Aug 21, 2007
Loading...

Share This Page