Re: virtual method hidden
Re: virtual method hidden
- Subject: Re: virtual method hidden
- From: "Andy O'Meara" <email@hidden>
- Date: Thu, 08 Dec 2005 17:39:39 -0500
- Thread-topic: virtual method hidden
> In that it isn't clear if it's calling the base1 method or if it's
> calling the base2 method with the default value for the 2nd parm. But
> the 2 methods in my previous example have no default values, and it's
> really clear that the 2 are quite different.
Yep, no doubt that it's any annoying language spec. Over the years,
whenever I'm in that situation, I do the following:
class Base {
public:
void Foo( void* inPtr ) { Foo_Impl( -1, inPtr ); }
void Foo( int x ) { Foo_Impl( x, NULL ); }
protected:
virtual void Foo_Impl( int x, void* inPtr );
};
class Blah {
protected:
virtual void Foo_Impl( int x, void* inPtr );
};
It's a little more wordy, but it buys you public name overloading and it
saves you from getting crazy with the 'ol scope operator (making the code
less pliable). You also don't pick up any overhead since the compiler will
be smart enough to inline the calls to Foo_Impl. I also like this trick
because you end up with a 'master' Foo_Impl() fcn with more general/private
parameters (and the public fcns just do any trivial arg
conversion/substitution such as I have above).
> I'm sure that CW would also warn about it; that's where I first learned
> about the concept. It's pretty easy to insert a "using" clause to avoid
> the problem, hence my recommendation to google "rather annoying..."
Yeah CW definitely has this warning--it's saved my bacon many times (Sean,
if you don't get the warning, you probably have it disabled--I strongly
recommend you have it on for all builds).
Andy
>
> -----Original Message-----
> On 2005-12-08 13:29, Steve Mills said:
>
>> And a base class:
>>
>> class fFolderWatcher {
>> public:
>> virtual OSErr WatchThisFolder(const short vrn, const
> long id);
>> virtual OSErr WatchThisFolder(const FolderSpec& fold);
>> };
>>
>> And a subclass of that:
>>
>> class dGraphicsFolderWatcher : public fFolderWatcher {
>> public:
>> virtual OSErr WatchThisFolder(const short vrn, const
> long id);
>> };
>>
>> The warning is that the base class method is being hidden by the
>> subclass method:
>>
>> fFolderWatcher.h:56: warning: 'virtual OSErr
>> fFolderWatcher::WatchThisFolder(const FolderSpec&)' was hidden
>> dFolderWatchers.h:29: warning: by 'virtual OSErr
>> dGraphicsFolderWatcher::WatchThisFolder(short int, long int)'
>>
>> I don't see how this is possible, unless the compiler is
>> deconstructing the FolderSpec into its parts, which just happen to be
>> a short and a long. But why wouldn't it find the base class method
>> that actually takes (short, long)?
>
> What David said. This is a warning gcc gives that CW does not, and I
> came across it recently too. From what I learned googling, one should
> basically not override overloaded methods. Or if you do, you have to
> override all of them, ie your dGraphicsFolderWatcher needs to override
> WatchThisFolder(const FolderSpec& fold), you could just call super in
> your override.
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden