| ftp.delorie.com/archives/browse.cgi | search |
| From: | horst DOT kraemer AT t-online DOT de (Horst Kraemer) |
| Newsgroups: | comp.os.msdos.djgpp |
| Subject: | Re: Possible Compiler Bug |
| Date: | Sat, 26 Feb 2000 11:33:09 GMT |
| Organization: | T-Online |
| Lines: | 119 |
| Message-ID: | <38b7b14d.68122098@news.btx.dtag.de> |
| References: | <38B57379 DOT 817FFBE4 AT netcom DOT ca> |
| Mime-Version: | 1.0 |
| X-Trace: | news05.btx.dtag.de 951564728 24422 0306239354-0001 000226 11:32:08 |
| X-Complaints-To: | abuse AT t-online DOT de |
| X-Sender: | 0306239354-0001 AT t-dialin DOT net |
| X-Newsreader: | Forte Free Agent 1.11/32.235 |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
| Reply-To: | djgpp AT delorie DOT com |
On Thu, 24 Feb 2000 13:07:53 -0500, MM <mm AT netcom DOT ca> wrote:
> #include <iostream.h>
>
> class t
> {
> private:
> int a;
> public:
> t (void) : a (10) { };
>
> void selectfunc (int w);
> void multifunc (void);
> void (t::*funcptr)(void);
>
> void func1 (void);
> void func2 (void);
> void func3 (void);
> };
>
> void t::func1 (void) { cout << "Func1 " << a << endl; }
> void t::func2 (void) { cout << "Func2 " << a << endl; }
> void t::func3 (void) { cout << "Func3 " << a << endl; }
>
> void t::selectfunc (int w)
> {
> switch (w)
> {
> case 1: funcptr = &func1; break;
> case 2: funcptr = &func2; break;
> case 3: funcptr = &func3; break;
> }
> }
>
> void t::multifunc (void)
> {
> this->funcptr ();
> }
>
> int main (void)
> {
>
> return (0);
> }
1) It is certainly a bug if a compiler crahes upon an unexpected
situation and illegal code.
2) As to your question if you are doing something wrong:
The statement
this->funcptr();
is ill-formed and meaningless. What you want is
void t::multifunc (void)
{
(this->*funcptr)();
}
If you want to call a member function through an object and a pointer
to a member function you have to apply the operator .* (for objects)
or ->* for pointers to objects and you have to put the expression
(obj.*membfuncptr) or (objptr->*membfuncptr)
into brackets because the operators .* and ->* have a lower
precendence than the function call operator () - while the "normal"
operators . and -> have the same precedence as (). Thus
this->func()
would be interpreted automatically as
(this->func)()
- which is what you mean - but
this->*funcptr()
would be interpreted as
this->*(funcptr())
which is (usually) not what you mean. Note that .* and ->* are not
"combinations" of the operators (. -> *). They are independent
operators of their own and don't inherit properties from their lexical
components (. -> *).
And please note that ->* or .* do _not_ automatically introduce the
scope of the left operand like the operators (. ->). I.e. in this
context
struct X
{
void (X::*mfp)();
};
void f()
{
X* px = new X;
(px->*mfp)(); // Error
}
would be an error because there is no mfp in scope. The correct call
in order to specify the pointer px->mfp would be
(px->*px->mfp)();
Regards
Horst
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |