Template Inheritance Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jayesah@gmail.com

    #1

    Template Inheritance Problem

    I have following code:

    #include <iostream>
    using namespace std;

    template <class Tclass Parent
    {
    public:
    void fun(const T a){ }
    };

    class myclass: public Parent<int*>
    {
    public:
    void myclassfun(cons t int* a)
    {
    Parent<int*>::f un(a);
    }
    };
    int main() {
    myclass obj1;
    int * ptr;
    obj1.myclassfun (ptr);
    return 0;
    }

    This gives me compilation error:
    "In member function `void myclass::myclas sfun(const int*)':
    error: invalid conversion from `const int*' to `int*'
    error: initializing argument 1 of `void Parent<T>::fun( T) [with T =
    int*]' "

    Please tell me what is the problem here ?
    I am not allowed to change Parent class interface.

    Jayesh

  • Ian Collins

    #2
    Re: Template Inheritance Problem

    jayesah@gmail.c om wrote:
    I have following code:
    >
    #include <iostream>
    using namespace std;
    >
    template <class Tclass Parent
    {
    public:
    void fun(const T a){ }
    };
    >
    class myclass: public Parent<int*>
    {
    public:
    void myclassfun(cons t int* a)
    {
    Parent<int*>::f un(a);
    just write fun(a);
    }
    };
    int main() {
    myclass obj1;
    int * ptr;
    obj1.myclassfun (ptr);
    return 0;
    }
    >
    This gives me compilation error:
    "In member function `void myclass::myclas sfun(const int*)':
    error: invalid conversion from `const int*' to `int*'
    error: initializing argument 1 of `void Parent<T>::fun( T) [with T =
    int*]' "
    >
    Please tell me what is the problem here ?
    Nothing to do with templates, you are passing a pointer to const int to
    a function that expects a const int pointer. The two are different
    types, the former is a mutable pointer to an immutable object, the
    latter is an immutable pointer to a mutable objects.

    --
    Ian Collins.

    Comment

    • h.yuzhao@gmail.com

      #3
      Re: Template Inheritance Problem


      "Ian Collins дµÀ£º
      "
      jayesah@gmail.c om wrote:
      I have following code:

      #include <iostream>
      using namespace std;

      template <class Tclass Parent
      {
      public:
      void fun(const T a){ }
      };

      class myclass: public Parent<int*>
      {
      public:
      void myclassfun(cons t int* a)
      {
      Parent<int*>::f un(a);
      >
      just write fun(a);
      >
      }
      };
      int main() {
      myclass obj1;
      int * ptr;
      obj1.myclassfun (ptr);
      return 0;
      }

      This gives me compilation error:
      "In member function `void myclass::myclas sfun(const int*)':
      error: invalid conversion from `const int*' to `int*'
      error: initializing argument 1 of `void Parent<T>::fun( T) [with T =
      int*]' "

      Please tell me what is the problem here ?
      >
      Nothing to do with templates, you are passing a pointer to const int to
      a function that expects a const int pointer. The two are different
      types, the former is a mutable pointer to an immutable object, the
      latter is an immutable pointer to a mutable objects.
      >
      --
      Ian Collins.
      No, I can't exactly agree with you...:)
      I think this was caused by that int* and const int* are not the same.
      Immutable pointer to int is declared as int* const p; but immutable int
      of pointer shoud be const int* p.

      Comment

      • Ian Collins

        #4
        Re: Template Inheritance Problem

        h.yuzhao@gmail. com wrote:
        "Ian Collins дµÀ£º
        >>
        >>Nothing to do with templates, you are passing a pointer to const int to
        >>a function that expects a const int pointer. The two are different
        >>types, the former is a mutable pointer to an immutable object, the
        >>latter is an immutable pointer to a mutable objects.
        >>
        >
        No, I can't exactly agree with you...:)
        I think this was caused by that int* and const int* are not the same.
        Immutable pointer to int is declared as int* const p; but immutable int
        of pointer shoud be const int* p.
        >
        Sorry, but the function sig was void fun(const T a) which instantiates
        to int* const, a constant pointer, not a pointer to const.

        --
        Ian Collins.

        Comment

        Working...