473,387 Members | 1,899 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,387 software developers and data experts.

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 2598
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::Call;

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::base::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.google.c om...
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::Call;
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::base::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******@nospam.mvps.org> wrote in message
news:OS**************@tk2msftngp13.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::Call;
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::base::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******@nospam.mvps.org> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
Jasper Kent wrote:
"Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org> wrote in message
news:OS**************@tk2msftngp13.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***********@virgilio.it (marco_segurini)
Newsgroups: microsoft.public.dotnet.languages.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**********@google.com
NNTP-Posting-Date: 8 Sep 2003 09:55:55 GMT
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!newsfeed.frii.net!newsfeed.frii.net!140.99.99.19 4.MISMATCH!newsfeed1.easyn
ews.com!easynews.com!easynews!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-05!sn-xit
-09!supernews.com!postnews1.google.com!not-for-mailXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:28042
X-Tomcat-NG: microsoft.public.dotnet.languages.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
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'...
16
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...
2
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...
9
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...
9
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
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...
2
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...
3
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...
3
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( )....
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.