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

Pointers to Members

I want to implement dynamic calling of interface methods using pointers to
members and have some trouble to get it right. An example of what I am
talking about looks like that:

///////////////////////////////////////////////
class StdInterface {
public:
int func(void) {return 1;}
int func2(void) {return 2;}
};

typedef int (StdInterface::*fp)();

class UseInterface {
public:
fp m_func;
};
///////////////////////////////////////////////

The next code excerpt shows how I want to use the pointer to meber:

///////////////////////////////////////////////
UseInterface *a = new UseInterface();
StdInterface *b = new StdInterface();

std::cout << b->func();
a->m_func = &StdInterface::func;

// here the compiler error C2065: 'm_func': not declared occurs (MS VC
..net):
std::cout << (a->*m_func)();
///////////////////////////////////////////////

Why does the compiler complain here? I have found an example of how to use a
pointer to member in Stroustrup (15.5 - Pointers to Members):

///////////////////////////////////////////////
//Example : Stroustrup (15.5 : Pointers to Members)

typedef int (StdInterface::*Pstd_mem)(); // pointer to member type

void f(StdInterface* p)
{
Pstd_mem s = &StdInterface::func;
//p->func(); // direct call
std::cout << (p->*s)() ; // call through pointer to member
}
////////////////////////////////////////////////

The above code works fine while mine is somehow wrong and I don't have a
clue on how to fix it. The only difference is, that my pointer is inside a
class, while the pointer in Stroustrups example is somehow
"function-scoped"...

Any help would be very much appreciated!

Jul 22 '05 #1
1 1252
On Sun, 1 Feb 2004 21:57:44 +0100 in comp.lang.c++, "Christian Nolte"
<ch******@web.de> was alleged to have written:
I want to implement dynamic calling of interface methods using pointers to
members and have some trouble to get it right.
For a start, see section "[33] Pointers to member functions" in Marshall
Cline's C++ FAQ. It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
// here the compiler error C2065: 'm_func': not declared occurs (MS VC
.net):
std::cout << (a->*m_func)();


Invoking the function requires an instance of class StdInterface.
b->*

m_func is a ordinary member of class UseInterface.
a->m_func

Put them together
b->*(a->m_func)()

Jul 22 '05 #2

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
4
by: Oystein Haare | last post by:
Is it best to declare class member objects as pointers or not pointers? class A { public: A(int i); //.... };
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
42
by: x-pander | last post by:
Is is guaranteed, that a pointer to any object in C points exactly at the lowest addressed byte of this object? Specifcally is it possible for any platform/os/compiler combination that: (char...
3
by: m | last post by:
Hi, I'm reading Microsoft Visual C++ .NET Step by Step, Version 2003 and I found a sentence it says : "you can't have pointers to managed types as members of a __gc class.". Why ? Thanks,...
20
by: Joe Van Dyk | last post by:
Is there some rule of thumb about when to use pointers to an object and when to use a reference* to an object when a class needs to have objects as data members? Example: class A { B* b_ptr;...
66
by: Praveen | last post by:
Hi, I came across a program as follows main() { int x, y ; int *p1 = &x; int *p2 = &y; printf("%d\n", p1-p2); }
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
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...
0
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...

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.