Can my class tell if one of its virtual members is overloaded?

  • Thread starter Thread starter Tony Maresca
  • Start date Start date
T

Tony Maresca

My class:

class foo
{
protected virtual void MyMember()
{
}
}

When this class is a base with any number of derivatives,
is there a way for it to tell if the virtual member function
is overloaded in any derived class, including the concrete
instance class?


--
Tony M.
 
Tony said:
When this class is a base with any number of derivatives,
is there a way for it to tell if the virtual member function
is overloaded in any derived class, including the concrete
instance class?

Certainly not in the generic way you are asking, simply because the base
class doesn't know anything about derived classes that exist. How would
it? The derived classes could be all over the place, possibly in another
assembly.

I imagine that it might be possible to find something out with
Reflection at the point where you already know that a specific
overridden method is being called (or you are in that method, so to
speak), but that doesn't sound like what you're asking.

What are you trying to do?


Oliver Sturm
 
Tony Maresca said:
When you say "the base class doesn't know anything
about the derived classes",

Um, not sure about that. If my base class can get the
type of an instance of a derived class, how can we say
that a base class cannot discover anything about derived
classes, at runtime?

Are you absolutely sure about that?

The point is that you can never know what all the derived classes of a
type are, unless it's internal. So although all the created instances
of derived types may not override a member, that doesn't mean there
isn't another type around which *does* override a member.
 
Tony Maresca said:
Sorry, perhaps I didn't state the original problem
correctly:

I want to know if the derived class(es) of an
instance overrides a protected virtual member
of my base.

The purpose of knowing this, is because in some
cases, there needs to be some work done before
calling a protected virtual member. But, if the
purpose of calling it from a base class is to serve
as a form of notification to derived classes, then
there is no point to calling it if no derived classes
of a given instance, are overriding it.

Hence, I can avoid any work that must be done
in preperation for calling the virtual member, if
there is no purpose to calling it.

In that case, I *believe* that Type.GetMethod, specifying
BindingFlags.DeclaredOnly will do what you want - but you'll need to
call it on each type in the hierarchy below your base class.

The fact that you phrase it in terms of notification suggests that
perhaps this should be an event that derived types could subscribe to -
then it's just a quick nullity check...
I use similar optimizations to avoid the overhead
of preparing to call events, by looking at the list
of delegates before doing any preperation (e.g.,
the event arguments). E;g., if GetInvocationList()
returns an empty array, I can avoid preparing the
event arguments and any other work that must be
done in support of firing the event.

GetInvocationList() will ever return an empty array - a
MulticastDelegate always consists of *one or more* delegates. You just
need to test for nullity.
 
Tony said:
When you say "the base class doesn't know anything
about the derived classes",

Um, not sure about that. If my base class can get the
type of an instance of a derived class, how can we say
that a base class cannot discover anything about derived
classes, at runtime?

Are you absolutely sure about that?

I was assuming you asked about a feature specific to your requirements.
Of course, every class can use Reflection to find out everything about
every other class out there.



Oliver Sturm
 
Just out of curiosity: my newsreader doesn't show me the message that
Jon is replying to here. Is that a problem in my newsreader or wasn't
Tony's message posted to the group for some reason?


In that case, I *believe* that Type.GetMethod, specifying
BindingFlags.DeclaredOnly will do what you want - but you'll need to
call it on each type in the hierarchy below your base class.

The fact that you phrase it in terms of notification suggests that
perhaps this should be an event that derived types could subscribe to -
then it's just a quick nullity check...




GetInvocationList() will ever return an empty array - a
MulticastDelegate always consists of *one or more* delegates. You just
need to test for nullity.




Oliver Sturm
 
Oliver said:
Just out of curiosity: my newsreader doesn't show me the message that
Jon is replying to here. Is that a problem in my newsreader or wasn't
Tony's message posted to the group for some reason?

Tony's message was posted all right. Perhaps thunderbird is a little
buggy in this. I found thunderbird not that great after a while and
went back to XanaNews

FB


--
 

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

Back
Top