473,734 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get parent class function pointer

Hi,

the following sample code shows a compiler error I get trying to build
some old code with the last CL compiler (vers 13.10.3077):

//----- begin
#include <iostream>

namespace ns
{
class base
{
public:
base() {}
virtual ~base() {}
protected:
void Call() { std::cout << "OK" << std::endl; }
};

class derived : public base
{
public:
void Start()
{
typedef void (ns::base::* typeCall)(void) ;
typeCall fnCall;
fnCall = &ns::base::Call ; // this is the line # 21
(this->*fnCall)();
}
};
}
int main()
{
ns::derived d;
d.Start();

return 0;
}
//----- end

I get the following compiler message error at line 21:

main.cpp(21): error C2248: 'ns::base::Call ' : cannot access protected
member declared in class 'ns::base'

Workaround: if I remove the ampersand all works fine.

I like to know if the line # 21 is really illegal.

thanks.
Marco.
Nov 16 '05 #1
6 2625
I wouldn't like to trawl through the standard on this one, but I suspect
that the error is correct.

A derived class method is only allowed to access protected members in base
when base is acting as derived's base class. Thus:

class derived : public base
{
public:
void Start()
{
derived objA;
objA.Call (); // Compiles OK

base objB; // Or a different derived class
objB.Call (); // Compiler error
}
};

So to make your code compile, replace:

fnCall = &ns::base::Call ;

with

fnCall = &ns::derived::C all;

which works even though Call () is not overridden in derived.

If you do have a derived implementation of Call, and you still want
base::Call, then

fnCall = &ns::derived::b ase::Call;

will do the trick, but only if Call () is non-virtual. If it is virtual, you
get derived::Call.

Hope this helps,

Jasper Kent.
"marco_segurini " <ma***********@ virgilio.it> wrote in message
news:a3******** *************** ***@posting.goo gle.com...
Hi,

the following sample code shows a compiler error I get trying to build
some old code with the last CL compiler (vers 13.10.3077):

//----- begin
#include <iostream>

namespace ns
{
class base
{
public:
base() {}
virtual ~base() {}
protected:
void Call() { std::cout << "OK" << std::endl; }
};

class derived : public base
{
public:
void Start()
{
typedef void (ns::base::* typeCall)(void) ;
typeCall fnCall;
fnCall = &ns::base::Call ; // this is the line # 21
(this->*fnCall)();
}
};
}
int main()
{
ns::derived d;
d.Start();

return 0;
}
//----- end

I get the following compiler message error at line 21:

main.cpp(21): error C2248: 'ns::base::Call ' : cannot access protected
member declared in class 'ns::base'

Workaround: if I remove the ampersand all works fine.

I like to know if the line # 21 is really illegal.

thanks.
Marco.

Nov 16 '05 #2
marco_segurini wrote:
Thank you very much.

A last question: why

fnCall = /*&*/ns::base::Call; // line # 21 without ampersand

compile fine?


According to the standard, it should not. It's an MS extension to accept
it. Try compiling with -Za & see if it's then flagged as an error.

-cd
Nov 16 '05 #3
Jasper Kent wrote:
I wouldn't like to trawl through the standard on this one, but I
suspect that the error is correct.

A derived class method is only allowed to access protected members in
base when base is acting as derived's base class. Thus:

class derived : public base
{
public:
void Start()
{
derived objA;
objA.Call (); // Compiles OK

base objB; // Or a different derived class
objB.Call (); // Compiler error
}
};

So to make your code compile, replace:

fnCall = &ns::base::Call ;

with

fnCall = &ns::derived::C all;
Correct.

which works even though Call () is not overridden in derived.

If you do have a derived implementation of Call, and you still want
base::Call, then

fnCall = &ns::derived::b ase::Call;


This should produce the same error as the original - ns::base::Call is not
accessible through a pointer or reference to ns::base.

-cd
Nov 16 '05 #4

"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:OS******** ******@tk2msftn gp13.phx.gbl...
Jasper Kent wrote:
I wouldn't like to trawl through the standard on this one, but I
suspect that the error is correct.

A derived class method is only allowed to access protected members in
base when base is acting as derived's base class. Thus:

class derived : public base
{
public:
void Start()
{
derived objA;
objA.Call (); // Compiles OK

base objB; // Or a different derived class
objB.Call (); // Compiler error
}
};

So to make your code compile, replace:

fnCall = &ns::base::Call ;

with

fnCall = &ns::derived::C all;
Correct.

which works even though Call () is not overridden in derived.

If you do have a derived implementation of Call, and you still want
base::Call, then

fnCall = &ns::derived::b ase::Call;


This should produce the same error as the original - ns::base::Call is not
accessible through a pointer or reference to ns::base.


Well it compiles fine on VC++ v7.

-cd

Nov 16 '05 #5
If it's good enough for you, a VC++ MVP, then why isn't it good enough for
VC++ compiler itself?

"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:ug******** ******@TK2MSFTN GP12.phx.gbl...
Jasper Kent wrote:
"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:OS******** ******@tk2msftn gp13.phx.gbl...

This should produce the same error as the original - ns::base::Call
is not accessible through a pointer or reference to ns::base.


Well it compiles fine on VC++ v7.


The gold standard of the Standard, Comeau, rejects it though. Without
spending a lot of time wading through the turgid verbiage of the standard,
that's good enough for me. It's definitely a dark corner though.

-cd

Nov 16 '05 #6

--------------------
From: ma***********@v irgilio.it (marco_segurini )
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Subject: Get parent class function pointer
Date: 8 Sep 2003 02:55:54 -0700
Organization : http://groups.google.com/
Lines: 53
Message-ID: <a3************ **************@ posting.google. com>
NNTP-Posting-Host: 62.110.81.102
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google. com 1063014955 12167 127.0.0.1 (8 Sep 2003 09:55:55 GMT)X-Complaints-To: gr**********@go ogle.com
NNTP-Posting-Date: 8 Sep 2003 09:55:55 GMT
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new s-out.cwix.com!ne wsfeed.cwix.co
m!newsfeed.frii .net!newsfeed.f rii.net!140.99. 99.194.MISMATCH !newsfeed1.easy n
ews.com!easynew s.com!easynews! sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-05!sn-xit
-09!supernews.co m!postnews1.goo gle.com!not-for-mailXref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:28042
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vc

Hi,

the following sample code shows a compiler error I get trying to build
some old code with the last CL compiler (vers 13.10.3077):

//----- begin
#include <iostream>

namespace ns
{
class base
{
public:
base() {}
virtual ~base() {}
protected:
void Call() { std::cout << "OK" << std::endl; }
};

class derived : public base
{
public:
void Start()
{
typedef void (ns::base::* typeCall)(void) ;
typeCall fnCall;
fnCall = &ns::base::Call ; // this is the line # 21
(this->*fnCall)();
}
};
}
int main()
{
ns::derived d;
d.Start();

return 0;
}
//----- end

I get the following compiler message error at line 21:

main.cpp(21) : error C2248: 'ns::base::Call ' : cannot access protected
member declared in class 'ns::base'

Workaround: if I remove the ampersand all works fine.

I like to know if the line # 21 is really illegal.

thanks.
Marco.


Here's the reason from the ISO Standard:

[class.protected] 11.5 Protected member access
1 When a friend or a member function of a derived class references a
protected nonstatic member function or
protected nonstatic data member of a base class, an access check applies in
addition to those described earlier
in clause 11.102) Except when forming a pointer to member (5.3.1), the
access must be through a
pointer to, reference to, or object of the derived class itself (or any
class derived from that class) (5.2.5). If
the access is to form a pointer to member, *** the nested-name-specifier
shall name the derived class (or any
class derived from that class). ***

Note the part marked with "***". This is what the compiler is complaining
about.

--
Tanveer Gani, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Nov 16 '05 #7

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

Similar topics

5
27600
by: Paul | last post by:
Hi all, Here is what I am trying to do. I have a parent class calling a child class. when one function in child class is called, i need to call parent class' function. How can I get parent class' pointer? Thx Paul
16
2671
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
2
1918
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
9
5122
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In half-code this should in the end look like this: In the child class:
9
2083
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
6
7964
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a particular struct (this struct is dynamically allocated by another provided function). To avoid memory
2
5310
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the pointer to the parent node, the program will be inefficient and slow. It works fine at first. However, when I started to build the remove function, it destroys the tree when I delete a node. I already changed the parent pointer whenever I delete a...
3
11877
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a to-be-developed-function cmdOkay_Click() function as described below. 1. Create an user control that contains an OK button as below Public Class MyButton:System.Windows.Form.UserControl
3
1190
by: Peteroid | last post by:
Is it possible to make a public parent class method unavailable (i.e., generate an error at compile time) to a particular child class? For example, say a parent class has a public method Add( ). I want to create a child class of this parent class that does not have an Add( ) method (while possibly another child class does). I think I figured it out while writing this, so tell me if this is the 'standard method'. Make the parent class...
0
8946
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
9449
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
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
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
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.