public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Problems invoking a member function through pointers.
@ 1998-12-21 21:09 adriang
  1998-12-21 21:37 ` Alexandre Oliva
  0 siblings, 1 reply; 3+ messages in thread
From: adriang @ 1998-12-21 21:09 UTC (permalink / raw)
  To: egcs

In the following code I would expect the output to be:
Base::M1
Base::M1
Base::M2
Base::M2

To my surprise, the last line of the output says
Derived::M2

Does the standard mandate this behaviour?
I've tried with both Microsoft compiler and egcs.

thanx



#include <iostream>

class Interface
{
public:
    virtual void M1(void) = 0;
    virtual void M2(void) = 0;
};

class Base : public Interface
{
public:
    void M1(void){ std::cout << "Base::M1\n"; }
    void M2(void){ std::cout << "Base::M2\n"; }


    void Check(void);
};



class Derived1 : public Base
{
public:
    void M2(void){ std::cout << "Derived::M2\n"; }
};

void Base::Check(void)
{
    void (Base::*pfn1)(void) = &Base::M1;
    Base::M1();
    (this ->* pfn1)();

    void (Base::*pfn2)(void) = &Base::M2;
    Base::M2();
    (this ->* pfn2)();
}



void main(void)
{
    Derived1 d1;

    d1.Check();
}



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problems invoking a member function through pointers.
  1998-12-21 21:09 Problems invoking a member function through pointers adriang
@ 1998-12-21 21:37 ` Alexandre Oliva
  0 siblings, 0 replies; 3+ messages in thread
From: Alexandre Oliva @ 1998-12-21 21:37 UTC (permalink / raw)
  To: adriang; +Cc: egcs

On Dec 22, 1998, <adriang@adriang.ce.mediaone.net> wrote:

> To my surprise, the last line of the output says
> Derived::M2

> Does the standard mandate this behaviour?

Yep.  invoking member functions through pointers to member functions
always result in virtual dispatching.

-- 
Alexandre Oliva  http://www.dcc.unicamp.br/~oliva  aoliva@{acm.org}
oliva@{dcc.unicamp.br,gnu.org,egcs.cygnus.com,samba.org}
Universidade Estadual de Campinas, SP, Brasil

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problems invoking a member function through pointers.
@ 1998-12-22 11:15 Mike Stump
  0 siblings, 0 replies; 3+ messages in thread
From: Mike Stump @ 1998-12-22 11:15 UTC (permalink / raw)
  To: adriang, egcs

> Date: Mon, 21 Dec 1998 23:08:06 -0600 (EST)
> From: <adriang@adriang.ce.mediaone.net>
> To: egcs@egcs.cygnus.com

> In the following code I would expect the output to be:
> Base::M1
> Base::M1
> Base::M2
> Base::M2

> To my surprise, the last line of the output says
> Derived::M2

This isn't the right group to learn C++ programming, comp.lang.c++ is
a better place.  Yes, this is what virtual means.  virtual means call
the most derived function in your class hierarchy.  If you don't want
that behavior, don't use virtual.

obj.Base::M2 () != obj .* (&Base::M2) ()

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1998-12-22 11:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-21 21:09 Problems invoking a member function through pointers adriang
1998-12-21 21:37 ` Alexandre Oliva
1998-12-22 11:15 Mike Stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).