473,883 Members | 1,703 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 2635
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
27608
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
2684
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
1925
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
5135
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
2094
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
7972
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
5313
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
11884
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
1194
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
9791
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
10414
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
9572
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...
1
7971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7125
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
5797
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
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4611
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
2
4218
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.