Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template Inheritance Problem

Reply
Thread Tools

Template Inheritance Problem

 
 
jayesah@gmail.com
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
h.yuzhao@gmail.com
Guest
Posts: n/a
 
      11-30-2006

"Ian Collins д
"
> (E-Mail Removed) 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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-30-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments