-
October 30th, 2009, 03:53 AM
#1
[RESOLVED] overwrite virtual template methods
Hi Folks,
I've got a template and the relevant code looks like this:
----
public:
virtual bool IsValid () const
{
return IsValid (mMemberValue);
}
protected:
virtual bool IsValid (int value)const
{
//Here ist a little code testing value.
}
----
IsValid() does no more than calling IsValid(int).
If I try now to overwrite IsValid(), the compiler complains, that my new IsValid() hides IsValid(int).
If I try to overwrite IsValid(int), it complains about hiding IsValid().
As we're using Borland C++ 6, I know I could make the compiler stop complaining by using HIDESBASE.
But this - IMHO - strange warning makes me a little bit nervous. I think the compiler should be able to differ both methods by signature - as it does with methods in "real" classes.
Or am I seeing sth. wrong?
Thanks in advance
chabba
-
October 30th, 2009, 05:59 AM
#2
Re: overwrite virtual template methods
I cannot say that for sure because you didn't post all the code.
The hidding situation is in accordance to the standard. This happens even for non-templates classes. The C++ lookup mechanism will not look further for in other scopes if it already finds a name match in the current class. Example:
Code:
struct A
{
bool IsValid(int i) const {return true;}
};
struct B : A
{
bool IsValid(int x, int y) const {return true;}
//Uncomment below to fix.
//using A::IsValid;
};
int main()
{
B b;
int i = 10;
bool x = b.IsValid(i); //Error, only IsValid(int, int) from B is visible.
/* ... */
}
One way to fix it is with a using directive. Just uncoment the part I mention above.
-
November 2nd, 2009, 03:31 AM
#3
Re: overwrite virtual template methods
Hi Itcmelo,
thanks for the response. But I think I didn't make clear, what the problem is.
So I try once more (I cannot provide the complete and "real" code because it's far too much)
Code:
//This is a template
template class A
{
public:
virtual bool IsValid () const
{
return IsValid (mMemberValue);
}
/*...*/
protected:
virtual bool IsValid (int value)const
{
/*....*/
}
private:
int mMemberValue;
};
//This is a class derived from template A
class B : public A
{
public:
virtual bool IsValid() const; //<<<<<- The error occurs here!
/*....*/
};
In the above case the compiler complains about B::IsValid() hiding A::IsValid(int).
If A was a class, this works. I did similar things a million times before.
So I guess this has something to do with the template...
chabba
-
November 2nd, 2009, 05:15 AM
#4
Re: overwrite virtual template methods
You can't redeclare it or it will confuse the compiler. You need to provide an actual implementation instead. You can do that either in the B class or below it somewhere.
-
November 2nd, 2009, 07:38 AM
#5
Re: overwrite virtual template methods
Hi originalfamousjohnsmith,
yes, the compiler IS confused. That's right. But the thing is, that template A provides an implementation for IsValid(). Is Valid() is virtual. Class B wants to overwrite IsValid() and implement
its own behaviour. That is nothing unusal.
(In the full source file template A offers more virtual functions, that had always been successfully overwritten).
The ONLY difference with IsValid() is, that IsValid() exists twice in the template with a different signature. And the compiler complains about hiding the base method with the OTHER signature.
If I overwrite IsValid(), it complains about hiding IsValid(int)! And the other way round. This is, what I do not understand. So far, this seems to me that this might be a bug in the compiler (what is not very suprising, because it's B*RL*ND C++).
-
November 2nd, 2009, 12:11 PM
#6
Re: overwrite virtual template methods
Originally Posted by chabbawoggie
In the above case the compiler complains about B::IsValid() hiding A::IsValid(int).
If A was a class, this works. I did similar things a million times before.
So I guess this has something to do with the template...
The code you posted is not valid C++, so I can't be sure, but I don't think this is about templates at all. Did you try the solution ltcmelo suggested (adding a using statement to prevent the function from being hidden)?
Also, what do you mean with "If A was a class"? Is A not a class in your example? If so, what is it?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 2nd, 2009, 03:37 PM
#7
Re: overwrite virtual template methods
The compiler is correct. In C++, if a derived class declares a function with the same name, it hides the base class version, even if the signature differs.
-
November 2nd, 2009, 04:52 PM
#8
Re: overwrite virtual template methods
Originally Posted by chabbawoggie
So I try once more (I cannot provide the complete and "real" code because it's far too much)
Every compiler error can be reproduced with a minimal example. Instead of us having to come up with code, maybe you can post a real example that demonstrates the error. Right now, you're only concluding that something is wrong with the compiler -- we will know well enough if this is the case with real code being posted.
Code:
//This is a template
template class A
You could have started by providing the proper syntax for a C++ template. Then just add dummy, empty functions with bogus return values, i.e. enough so the code compiles and you get an error.
As a matter of fact, that is exactly what you should have done yourself even before posting, so as to possibly solve the problem yourself. Anytime I get a compiler error, I break it down to the simplest, bogus, but compilable example, and see if the compiler accepts the code. If it doesn't compile, OK, I have a small piece of code to test on another compiler. If that same piece of code compiles on another compiler, I have some code that I can post here, or even give it to the compiler authors/engineers to see if it is valid or not.
Regards,
Paul McKenzie
-
November 2nd, 2009, 07:43 PM
#9
Re: overwrite virtual template methods
Originally Posted by rohshall
The compiler is correct. In C++, if a derived class declares a function with the same name, it hides the base class version, even if the signature differs.
Wait; we're dealing with virtual functions here. The hiding issue applies when a derived class implements a non-virtual function with the same name. In this case, the derived class is attempting to override a virtual function that it has inherited which ought to be fine (although I am confused as to what could possibly be overridden in this case). The base class also overloads the virtual function with another one that takes an input parameter. I'm not quite sure I understand that design but I don't see any problem with the idea of doing that. We should be able to overload virtual functions with different combinations of parameters.
Paul is correct. Something isn't adding up. We need to see a more complete example that demonstrates the problem so that we can try compiling with our compilers.
-
November 2nd, 2009, 07:45 PM
#10
Re: overwrite virtual template methods
Originally Posted by ltcmelo
I cannot say that for sure because you didn't post all the code.
The hidding situation is in accordance to the standard. This happens even for non-templates classes. The C++ lookup mechanism will not look further for in other scopes if it already finds a name match in the current class. Example:
Code:
struct A
{
bool IsValid(int i) const {return true;}
};
struct B : A
{
bool IsValid(int x, int y) const {return true;}
//Uncomment below to fix.
//using A::IsValid;
};
int main()
{
B b;
int i = 10;
bool x = b.IsValid(i); //Error, only IsValid(int, int) from B is visible.
/* ... */
}
One way to fix it is with a using directive. Just uncoment the part I mention above.
I don't understand this example. the OP posted declarations that were virtual. You are talking about the hiding issue with non-virtual functions which is a totally different kind of problem isn't it?
-
November 2nd, 2009, 11:04 PM
#11
Re: overwrite virtual template methods
Forgive me if my terminology is off, but basically you need to 'specialize' the template for that implementation. I've had the exact problem, assuming this is being explained at all well.
So if you have:
Code:
template <typename type> class Base
{
void someFunc(){}
};
class Child: public Base<type>
{
}
template Base<type>::someFunc()
{
}
-
November 3rd, 2009, 03:53 AM
#12
Re: overwrite virtual template methods
Ok, here is a "full featured" example. Meanwhile I installed VS Express. This compiles without any warning (except for warning level 4, where it complains about unreferenced parameters).
But Borland C++ says "B::IsValid()" const hides virtual function "A::<Dummy>IsValid(int) const".
Code:
//File TemplateA.h
template <class T> class A
{
public:
virtual bool IsValid() const
{
int i = 5;
return IsValid(i);
}
protected:
virtual bool IsValid(int value) const
{
return false;
}
};
//-------------------------------------------------
// File B.h
#include "TemplateA.h"
class Dummy
{
};
class B : public A<Dummy>
{
public:
B(void);
~B(void);
virtual bool IsValid() const;
};
//------------------------------------------------
//File B.cpp
#include "B.h"
B::B(void)
{
}
B::~B(void)
{
}
bool B::IsValid() const
{
return true;
}
I know, that it only produces "only" a warning with Borland, but I take warnings seriously and this special warning makes me a little nervous as I said before.
-
November 3rd, 2009, 06:16 AM
#13
Re: overwrite virtual template methods
Originally Posted by chabbawoggie
Ok, here is a "full featured" example. Meanwhile I installed VS Express. This compiles without any warning (except for warning level 4, where it complains about unreferenced parameters).
But Borland C++ says "B::IsValid()" const hides virtual function "A::<Dummy>IsValid(int) const".
Here is what Comeau C++ has to say:
Code:
Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 22: warning: overloaded virtual function
"A<T>::IsValid [with T=Dummy]" is only partially overridden in class
"B"
class B : public A<Dummy>
^
In strict mode, with -tused, Compile succeeded (but remember, the Comeau online compiler does not link).
Compiled with C++0x extensions enabled.
Regards,
Paul McKenzie
-
November 3rd, 2009, 12:57 PM
#14
Re: overwrite virtual template methods
Since we are only dealing with a warning, have you tried testing the class? Does it perform as expected? Have you created a unit test like application that calls the member functions in question and can you verify that the correct functions are called by setting breakpoints and stepping into the code with the debugger? Sorry, but I don't understand the warning that you are getting either.
-
November 3rd, 2009, 01:26 PM
#15
Re: overwrite virtual template methods
Originally Posted by kempofighter
I don't understand this example. the OP posted declarations that were virtual. You are talking about the hiding issue with non-virtual functions which is a totally different kind of problem isn't it?
Originally Posted by kempofighter
Sorry, but I don't understand the warning that you are getting either.
The name hiding applies whether the member functions are virtual or non-virtual. Consequently, a correct solution to the problem is:
Code:
class B : public A<Dummy>
{
public:
using A<Dummy>::IsValid;
B(void);
~B(void);
virtual bool IsValid() const;
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|