-
November 3rd, 2009, 02:05 PM
#16
Re: overwrite virtual template methods
I agree that qualifying the full name is the solution.
But I'm morw with kempofighter on this.
Originally Posted by itcmelo
Code:
int main()
{
B b;
int i = 10;
bool x = b.IsValid(i); //Error, only IsValid(int, int) from B is visible.
/* ... */
}
call to the b.IsValid was an error even before it made
because it was called on the object, not through a reference type.
so in this case,
my understanding is that it was the vTable lookup mechanism that called b.IsValid(i) invalid,
not the name hiding.
I believe Paul was right on this when he showed the comeau version of the error
Code:
#include <iostream>
template <typename T>
struct Base
{
Base() : x(T()) {}
virtual ~Base(){}
virtual void call(const T y) const
{
std::cout << x + y;
}
virtual void call() const
{
std::cout << x;
}
protected:
T x;
};
struct Derived : public Base<int>
{
virtual void call(const int y) const
{
std::cout << x + y;
}
};
int main()
{
Derived d;
Base<int>& ref = d;
ref.call(); // valid with a warning
}
Here, we're not using the using declaration.
I looked up Scott Meyer's effective C++,
and under "Never redefine an inherited non-virtual function",
he's also using reference types to illustrate the behavior
If name hiding does occur before name look up happens in virtual overload resolution,
(which I believe should happen afterwards)
then like kempofighter, I'm lost as to why.
Last edited by potatoCode; November 3rd, 2009 at 02:13 PM.
Reason: fixed some typo
-
November 3rd, 2009, 02:21 PM
#17
Re: overwrite virtual template methods
Originally Posted by potatoCode
call to the b.IsValid was an error even before it made
because it was called on the object, not through a reference type.
so in this case,
my understanding is that it was the vTable lookup mechanism that called b.IsValid(i) invalid,
not the name hiding.
No, it is the name hiding. If not for the effects of name hiding, there should be no problem since the derived class inherits the base class members. That the call is non-virtual is not important.
Originally Posted by potatoCode
If name hiding does occur before name look up happens in virtual overload resolution,
(which I believe should happen afterwards)
then like kempofighter, I'm lost as to why.
If name hiding should occur at that point, then it should result in an error, not a warning. The warning is to help the programmer identify a probable problem, which it does in this case.
-
November 5th, 2009, 03:58 AM
#18
Re: overwrite virtual template methods
Hi all,
I think I found the explanation. According to the standard, if you have methods of the same name with a different signature, you cannot overwrite only one. Instead you MUST implement them all.
Otherwise the remaining methods will be hidden.
I think, this is one of the issues that will be changed with the next standard (coming 2010 I think) . (For other languages this is no problem either.)
But so long - as often - it seems that it depends on the compiler you're using (and how old it is...)
chabba
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
|