BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Is virtual function redefined?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Fabrizio Villanova
Guest





PostPosted: Wed Nov 23, 2005 10:43 am    Post subject: Is virtual function redefined? Reply with quote



Hi,

in a base class i have a virtual function that does nothing. Derived classes
MAY redefine it to make something.
Base class has a button that i need to set it visible ONLY if the virtual
function is redefined.

There is a way to detect in the base class code that the actual instance
(the derived class) redefine the function and take the required actions?
Can i use function closures? How?

Thank you
Fabrizio



Back to top
Alan Bellingham
Guest





PostPosted: Wed Nov 23, 2005 11:03 am    Post subject: Re: Is virtual function redefined? Reply with quote



"Fabrizio Villanova" <fvillanova (AT) essedata (DOT) nospam.it> wrote:

Quote:
in a base class i have a virtual function that does nothing. Derived classes
MAY redefine it to make something.
Base class has a button that i need to set it visible ONLY if the virtual
function is redefined.
There is a way to detect in the base class code that the actual instance
(the derived class) redefine the function and take the required actions?

No.

Not actually true - the base class _could_ try seeing which possible
derived class is actually instantiated, but it _should_ not. You won't
know when you originally write it which derived classes will exist. So
every time you create a new derived class, you have to update the base
class. This is tedious. And it's error-prone in the extreme. It's one of
the worst sorts of coupling I can imagine.

No base class should know anything about the classes derived from it. It
may know that classes will be derived from it ('virtual' is a pretty
good clue!). But the derived classes already know about their bases, and
two way knowledge is overly-strong coupling.

So you shouldn't be trying.

It's far better to provide a way for the derived class to tell the base
class whether it would like the button to be shown. I can think of a
number of ways to do this. Here are two.

a) Provide a ShowButton() virtual function. The base implementation does
nothing. The derived shows the button.

b) Have a protected bool member in the base class. Have the base
constructor set it to false. Have the derived constructor set it
true. Have the base show the button if the flag is true.

Quote:
Can i use function closures? How?

Why do you think function closures would help? Or do you just think
they're something to do with the problem, because they are not.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK

Back to top
Fabrizio Villanova
Guest





PostPosted: Wed Nov 23, 2005 2:55 pm    Post subject: Re: Is virtual function redefined? Reply with quote



Quote:
Can i use function closures? How?

Why do you think function closures would help? Or do you just think
they're something to do with the problem, because they are not.

Alan Bellingham

i have tought to compare pointers like:

if(Base::Func!=Func)

where Base::Func points to the original (base) function and Func retrieve
the address from the vtable of the instance.
If Func is redefined the two address should be different.
i tried something about this but the result was wrong.

Fabrizio



Back to top
Rob
Guest





PostPosted: Thu Nov 24, 2005 7:17 am    Post subject: Re: Is virtual function redefined? Reply with quote

Fabrizio Villanova wrote:

Quote:
Can i use function closures? How?

Why do you think function closures would help? Or do you just think
they're something to do with the problem, because they are not.

Alan Bellingham

i have tought to compare pointers like:

if(Base::Func!=Func)

where Base::Func points to the original (base) function and Func
retrieve the address from the vtable of the instance.
If Func is redefined the two address should be different.
i tried something about this but the result was wrong.


You tought (sic!) incorrectly.

Pointers to member functions are computed relative to the class (eg an
offset), not absolutely. A consequence of that is (approximately)
that comparing Base::Func with Func will be comparing the locations
of Func within the virtual function table. Which means the test
Base::Func != Func will always give a false result.


Back to top
Alan Bellingham
Guest





PostPosted: Thu Nov 24, 2005 11:53 am    Post subject: Re: Is virtual function redefined? Reply with quote

Tom Widmer <tom_usenet (AT) hotmail (DOT) com> wrote:

Quote:
That seems to work on GCC. You should ask yourself whether this is
better than having a virtual boolean showButton() const; member function
though, because I don't think it is.

I don't think so either.

(Not that it compiled under BCB5, in case you were wondering.)

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK

Back to top
py
Guest





PostPosted: Fri Nov 25, 2005 4:35 pm    Post subject: Re: Is virtual function redefined? Reply with quote

Hello,

As the others said it's not possible, or very difficult - and u shouldn't
try !
But you could do something like that:

class MustBeOverrided {
public:
virtual void f() = 0;
};

class EmptyOverride : public MustBeOverrided {
public:
void f() { /* do nothing */ };
};

class AnOverride : public MustBeOverrided {
public:
void f() { /* do something */ };
};

U instanciate the class EmptyOverride, doing nothing, as u did with your
base class. U can do this so other users can't access the EmptyOverride and
inherits from it, so they have to inherits from MustBeOverrided. But bc the
abstract declaration they must override the function if they want
instanciate the class (c++ rule)

This way you even have no check to do, it's done at compile time.

s.pierre-yves (s.py)
[We want to make computers as intelligent as us: rapids like the flash and
never an error! Overflow error...]

"Fabrizio Villanova" <fvillanova (AT) essedata (DOT) nospam.it> a écrit dans le message
de news: [email]438447c1 (AT) newsgroups (DOT) borland.com[/email]...
Quote:
Hi,

in a base class i have a virtual function that does nothing. Derived
classes MAY redefine it to make something.
Base class has a button that i need to set it visible ONLY if the virtual
function is redefined.

There is a way to detect in the base class code that the actual instance
(the derived class) redefine the function and take the required actions?
Can i use function closures? How?

Thank you
Fabrizio




Back to top
Fabrizio Villanova
Guest





PostPosted: Fri Nov 25, 2005 4:44 pm    Post subject: Re: Is virtual function redefined? Reply with quote


"Tom Widmer" <tom_usenet (AT) hotmail (DOT) com> ha scritto nel messaggio
news:43859e3d (AT) newsgroups (DOT) borland.com...

Quote:
That seems to work on GCC. You should ask yourself whether this is better
than having a virtual boolean showButton() const; member function though,
because I don't think it is.

the example doesn't work as is in bcb6.
At this point, is better to set manually at design time the visible property
of the button than to try to automate it. I think.

thank you.

Fabrizio



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.