C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Returning ptr to a virtual member function of a base class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Dave Rahardja
Guest





PostPosted: Sun Dec 19, 2004 8:12 pm    Post subject: Returning ptr to a virtual member function of a base class Reply with quote



Consider the following example:

---

#include <iostream>

class Base
{
public:
virtual void fn() { std::cout << "Base::fn()n"; }
};

typedef void (Base::*Base_fn_ptr)();

class Derived: public Base
{
public:
virtual void fn() { std::cout << "Derived::fn()n"; }

static Base_fn_ptr Get_Base_Fn_Ptr() { return &Base::fn; }
};

int main()
{
Derived obj;
Base_fn_ptr ptr = Derived::Get_Base_Fn_Ptr();
(obj.*ptr)();

return 0;
}

---

Running the example causes "Derived::fn()" to be output. Is there a way
to craft Get_Base_Fn_Ptr() so that it returns the address of Base::fn()
instead of a virtual function-calling stub?

In other words, I want to defeat the polymorphic behavior of obj.

- Dave Rahardja
Back to top
GianGuz
Guest





PostPosted: Sun Dec 19, 2004 9:33 pm    Post subject: Re: Returning ptr to a virtual member function of a base cla Reply with quote



If I understand what you need, the way to defeat the polimorfic
behaviour of obj is to use the explicit qualification mechanism in
calling fn.

In your code you only have to call:

obj.Base::fn(); // explicit call to Base fn()
obj.Derived::fn(); // explicit call to Derived fn()

Gianguglielmo

Back to top
Jonathan Mcdougall
Guest





PostPosted: Sun Dec 19, 2004 9:54 pm    Post subject: Re: Returning ptr to a virtual member function of a base cla Reply with quote



Dave Rahardja wrote:
Quote:
Consider the following example:

---

#include <iostream

class Base
{
public:
virtual void fn() { std::cout << "Base::fn()n"; }
};

typedef void (Base::*Base_fn_ptr)();

class Derived: public Base
{
public:
virtual void fn() { std::cout << "Derived::fn()n"; }

static Base_fn_ptr Get_Base_Fn_Ptr() { return &Base::fn; }
};

int main()
{
Derived obj;
Base_fn_ptr ptr = Derived::Get_Base_Fn_Ptr();
(obj.*ptr)();

return 0;
}

---

Running the example causes "Derived::fn()" to be output. Is there a way
to craft Get_Base_Fn_Ptr() so that it returns the address of Base::fn()
instead of a virtual function-calling stub?

In other words, I want to defeat the polymorphic behavior of obj.

By using pointer to member functions, no. This system was especially
made for these cases: to make sure wherever in the hierarchy you take an
address, the correct function gets called.

You are probably aware you can call it directly by qualifying the function :

obj->Base::fun();


However, look at that :

# include <iostream>

class Base
{
public:
void f()
{
std::cout << "in base::f()";
}

virtual void vf()
{
f();
}
};

class Derived : public Base
{
public:
virtual void vf()
{
std::cout << "in derived::vf()";
}
};


typedef void (Base::*Pf)();

Pf test1()
{
return &Base::f;
}

Pf test2()
{
return &Base::vf;
}

int main()
{
Base *b = new Derived;

Pf pf = test1();
(b->*pf)(); // in derived::vf()

pf = test2();
(b->*pf)(); // in base::vf()

}

This may or not be what you look for.


Jonathan

Back to top
Dave Rahardja
Guest





PostPosted: Mon Dec 20, 2004 4:50 am    Post subject: Re: Returning ptr to a virtual member function of a base cla Reply with quote

GianGuz wrote:
Quote:
If I understand what you need, the way to defeat the polimorfic
behaviour of obj is to use the explicit qualification mechanism in
calling fn.

In your code you only have to call:

obj.Base::fn(); // explicit call to Base fn()
obj.Derived::fn(); // explicit call to Derived fn()

Gianguglielmo


That's exactly what I'm looking for. Thanks! I guess the syntax of the
explicit call escaped me.

-dr

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.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.