non-virtual call to a virtual function using pointer to member

A

archimed7592

Hi.
for example i have base class A and dirved class B:

struct A
{
virtual void f() { std::cout << "A::f()" << std::endl; }
};

struct B : public A
{
void f() { std::cout << "B::f()" << std::endl; }
};

int main()
{
B b;
A *a = &b;
a->f(); // virtual call. calls B::f()
a->A::f(); // non-virtual call. calls A::f()
A::* pf;
pf = &A::f();
(a->*pf)(); // virtual call. calls B::f()
pf = /* ??? */; // what should i write here for desired effect(non-
virtual call)
(a->*pf)(); // non-virtual call. calls A::f()

return 0;
}
 
S

Salt_Peter

Hi.
for example i have base class A and dirved class B:

struct A
{
virtual void f() { std::cout << "A::f()" << std::endl; }

};

struct B : public A
{
void f() { std::cout << "B::f()" << std::endl; }

};

int main()
{
B b;
A *a = &b;
a->f(); // virtual call. calls B::f()
a->A::f(); // non-virtual call. calls A::f()
A::* pf;

not in C++ its not, try:

void (A::*pf)() = &A::f;
(a->*pf)();
pf = &A::f();
(a->*pf)(); // virtual call. calls B::f()
pf = /* ??? */; // what should i write here for desired effect(non-
virtual call)

Why? You have 2 interfaces to your class. One is via pointer to
base and the other is to the static type itself. Why are you
attempting to break your own interfaces? Provide a non-virtual
function instead and look up 'Non-Virtual idiom' for a solution
involving private virtual functions.

read:
http://www.gotw.ca/publications/mill18.htm
 
A

archimed7592

not in C++ its not, try:

void (A::*pf)() = &A::f;
(a->*pf)();


Why? You have 2 interfaces to your class. One is via pointer to
base and the other is to the static type itself. Why are you
attempting to break your own interfaces? Provide a non-virtual
function instead and look up 'Non-Virtual idiom' for a solution
involving private virtual functions.

read:http://www.gotw.ca/publications/mill18.htm

It is only task for my brain... nothing else ;)
Question is: is there Standard-conforming C++ code, that does "non-
virtual call" through pointer-to-member.
I think, answer is negative. But, maybe I'm wrong?
 
S

Salt_Peter

{ Edits: removed quoted clc++m banner. Please remove manually if your
software doesn't. -mod }

It is only task for my brain... nothing else ;)
Question is: is there Standard-conforming C++ code, that does "non-
virtual call" through pointer-to-member.
I think, answer is negative. But, maybe I'm wrong?

I already answered your question, if you prefer your interfaces to
perform a non-virtual call, then design your interface(s) in
consequence. One interface is provided via pointer to base, the other
is the object's interface.

#include <iostream>

class A
{
public:
virtual void vf()
{
std::cout << "A::vf()\n";
}
void f()
{
std::cout << "A::f()\t";
A::vf();
}
void foo()
{
std::cout << "A::foo()\t";
vf(); // virtual !!!
}
};

class B : public A
{
private:
void vf() { std::cout << "B::vf()\n"; }
public:
void f()
{
std::cout << "B::f()\t";
vf();
}
};

int main()
{
B b;
A *a = &b;

// B::vf() is private but *not* to a base pointer
// the virtual call is *not* using B's interface
a->vf(); // virtual, calls B::vf()
a->A::f(); // nv calls A::f(), A::vf()
a->A::foo(); // nv calls A::foo(), B::vf()

void (A::*pf)() = &A::vf;
(a->*pf)(); // virtual calls B::vf()

pf = &A::f;
(a->*pf)(); // nv calls A::f(), A::vf()

pf = &A::foo;
(a->*pf)(); // nv calls A::foo(), B::vf()
}

/*
B::vf()
A::f() A::vf()
A::foo() B::vf()
B::vf()
A::f() A::vf()
A::foo() B::vf()
*/
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top