473,666 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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++.m oderated. First time posters: Do this! ]

May 28 '07 #1
4 1937
On May 28, 3:19 pm, archimed7592 <archimed7...@g mail.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++.m oderated. 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...@g mail.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++.m oderated. 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++.m oderated. 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...@g mail.comwrote:
On May 29, 6:47 am, Salt_Peter <pj_h...@yahoo. comwrote:
On May 28, 3:19 pm, archimed7592 <archimed7...@g mail.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++.m oderated. 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++.m oderated. 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
1777
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 characters (like voyels with accents) are changed to wrong characters. b) If a program has non-ASCII characters, save or save as work when a new file is created, but if I open a file with non-ASCII characters and I modify it, then I can not save it...
17
1745
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 is a constant >misery, having to mangle identifiers while documenting and thinking >in languages other than English, merely because the Python notion of >letter is limited to the English subset. Granted, keywords and standard >library use...
12
4418
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 characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
5
6755
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
2839
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 to mean in this context. Can someone explain? TIA
3
2479
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
2935
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 events.. But the regular forms that are also open do not receive that event. This is true whether there are child forms open or not.
399
12791
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 to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
0
1660
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
3464
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 me. Thanks. 1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference. 2. A non-const reference can...
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.