Template Inheritance Problem

Discussion in 'C++' started by jayesah@gmail.com, Nov 30, 2006.

  1. Guest

    I have following code:

    #include <iostream>
    using namespace std;

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

    class myclass: public Parent<int*>
    {
    public:
    void myclassfun(const int* a)
    {
    Parent<int*>::fun(a);
    }
    };
    int main() {
    myclass obj1;
    int * ptr;
    obj1.myclassfun(ptr);
    return 0;
    }

    This gives me compilation error:
    "In member function `void myclass::myclassfun(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
     
    , Nov 30, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > I have following code:
    >
    > #include <iostream>
    > using namespace std;
    >
    > template <class T> class Parent
    > {
    > public:
    > void fun(const T a){ }
    > };
    >
    > class myclass: public Parent<int*>
    > {
    > public:
    > void myclassfun(const int* a)
    > {
    > Parent<int*>::fun(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::myclassfun(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.
     
    Ian Collins, Nov 30, 2006
    #2
    1. Advertising

  3. Guest

    "Ian Collins дµÀ£º
    "
    > wrote:
    > > I have following code:
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > template <class T> class Parent
    > > {
    > > public:
    > > void fun(const T a){ }
    > > };
    > >
    > > class myclass: public Parent<int*>
    > > {
    > > public:
    > > void myclassfun(const int* a)
    > > {
    > > Parent<int*>::fun(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::myclassfun(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.
     
    , Nov 30, 2006
    #3
  4. Ian Collins Guest

    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.
     
    Ian Collins, Nov 30, 2006
    #4
    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. Chris Theis
    Replies:
    2
    Views:
    477
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    538
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,113
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    780
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    532
Loading...

Share This Page