473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

May 28 '07 #1
4 1917
On May 28, 3:19 pm, archimed7592 <archimed7...@gmail.comwrote:
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


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

May 29 '07 #2
On May 29, 6:47 am, Salt_Peter <pj_h...@yahoo.comwrote:
On May 28, 3:19 pm, archimed7592 <archimed7...@gmail.comwrote:
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

--
[ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
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?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

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

On May 29, 6:10 pm, archimed7592 <archimed7...@gmail.comwrote:
On May 29, 6:47 am, Salt_Peter <pj_h...@yahoo.comwrote:
On May 28, 3:19 pm, archimed7592 <archimed7...@gmail.comwrote:
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

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()
*/

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

May 30 '07 #4
archimed7592 wrote:
what should i write here for desired effect(non-virtual call)
{ Mod note: the following link describes a language extension for the
g++ compiler. -mod/aps }

there's a one trick ;)
http://gcc.gnu.org/onlinedocs/gcc-4....mber-functions
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

May 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bartolomé Sintes Marco | last post by:
Hi, I have downloaded and installed Python 2.3 RC1 in a Windows 98 SE computer. IDLE 1.0 does not work very well: a) When I open with IDLE 1.0 RC1 a program written with IDLE 0.8, non-ASCII...
17
by: Doug Fort | last post by:
This is an excerpt from a much longer post on the python-dev mailing list. I'm responding here, to avoid cluttering up python-dev. <snip> >Some English readers might not really imagine, but it...
12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: Lars | last post by:
Why doesn't the W3C's HTML Validator recognize &euro; and what do I have to do to make my html-file valid?
15
by: Sander Tekelenburg | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The HTML specs speak of "replaced" and "non-replaced" elements, yet for the life of me I can't find an explanation of what "replaced" is supposed...
3
by: Ivan | last post by:
Hi, how to filter out non-digit chars when user writes text to System.Windows.Forms.TextBox? Thanks, Iavmn
13
by: Academic | last post by:
I have a MDI form, sometimes child forms and sometimes forms that are neither If I close the app the child forms closing and closed event happens followed by the Mdi form receiving the...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: muzu1232004 | last post by:
If I have a table t1 with a column a as follows A 1 1 1 1 1 1 1
17
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.