Template Inheritance Problem

J

jayesah

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
 
I

Ian Collins

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.
 
H

h.yuzhao

"Ian Collins дµÀ£º
"
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.

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.
 
I

Ian Collins

"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.
Sorry, but the function sig was void fun(const T a) which instantiates
to int* const, a constant pointer, not a pointer to const.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top