avoiding virtual call with pointer to member function

X

xtrigger303

Hi to all,
a newbie question. Is it possible to avoid the virtual mechanism with
pointers to members syntax the same way it's done with an explicit
call ( obj.Base::Func() )? It would be useful for something I'm
doing... check out the trivial program down here...
Thanks in advance,
Francesco

#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base\n"; }
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der\n"; }
};

int main()
{
Der obj;

obj.Base::Do(); // avoid virtual call

void ( Base::*ptrToMembFunc )() const = &Base::Do;

( obj.*ptrToMembFunc )(); // virtual call
// is there a way to avoid virtual call with pointers to member
functions
// as in the above direct call?
}
 
V

Victor Bazarov

Hi to all,
a newbie question. Is it possible to avoid the virtual mechanism with
pointers to members syntax the same way it's done with an explicit
call ( obj.Base::Func() )? It would be useful for something I'm
doing... check out the trivial program down here...
Thanks in advance,
Francesco

#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base\n"; }
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der\n"; }
};

int main()
{
Der obj;

obj.Base::Do(); // avoid virtual call

void ( Base::*ptrToMembFunc )() const = &Base::Do;

( obj.*ptrToMembFunc )(); // virtual call
// is there a way to avoid virtual call with pointers to member
functions
// as in the above direct call?
}

You could try forcing 'obj' into being a 'Base' by means of
'static_cast'...

V
 
X

xtrigger303

Thanks for the reply, but doesn't work. I (guess) the vptr points
always to the same vtable.
Tried below


#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base\n"; }
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der\n"; }
};

int main()
{
Der obj;

obj.Base::Do(); // avoid virtual call

void ( Base::*ptrToMembFunc )() const = &Base::Do;

( obj.*ptrToMembFunc )(); // virtual call
// is there a way to avoid virtual call with pointers to member
functions
// as in the above direct call?

( static_cast< Base & >( obj ).*ptrToMembFunc )();
// won't work, the vptr always points to the same vtable

}
 
S

Salt_Peter

Thanks for the reply, but doesn't work. I (guess) the vptr points
always to the same vtable.

// top posting is not recommended

#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base::Do()\n"; }
void foo() const
{
std::cout << "Base::foo()\n";
Base::Do();
}
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der::Do()\n"; }
};

int main()
{
Der obj;
obj.Base::Do();

void ( Base::*ptrToMembFunc )() const = &Base::foo;
( obj.*ptrToMembFunc )();
}

/*
Base::Do()
Base::foo()
Base::Do()
*/
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top