473,499 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer to member function and pointer to constant member function

Are these pointers the same?

Fraser.
Jul 22 '05 #1
4 1929
On Sat, 14 Aug 2004 12:16:12 +0100, "Fraser Ross"
<fraserATmembers.v21.co.unitedkingdom> wrote:
Are these pointers the same?


No. Try this:

#include <iostream>
#include <ostream>

using namespace std;

class A
{
public:
void func() { cout << "Non-const func()." << endl; }
void func() const { cout << "Const func()." << endl; }
};

typedef void (A::* pf)();
typedef void (A::* cpf)() const;

int main()
{
A a;
const A b;
pf fp = &A::func;
cpf cfp = &A::func;

(a.*fp)(); // OK, outputs "Non-const func()."
(a.*cfp)(); // OK, outputs "Const func()."
// (b.*fp)(); // this line won't compile:
// "Cannot convert 'const A *' to 'A *'"
(b.*cfp)();
return 0;
}
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2
My compiler is working correctly with constness. const is usually on the
left of * so the syntax differs from the usual pointer syntax.

Fraser.
Jul 22 '05 #3
On Sat, 14 Aug 2004 14:08:31 +0100, "Fraser Ross"
<fraserATmembers.v21.co.unitedkingdom> wrote:
const is usually on the
left of * so the syntax differs from the usual pointer syntax.


What do you mean by that? Consider the following:

(1) const char * pcc;
(2) char * const cpc;
(3) const char * const cpcc;

Another equivalent (and possibly better, less ambiguous) way of
writing (1) and (3) would be:

char const * pcc;
char const * const cpcc;

Otherwise, they all mean something very different.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #4
Constant pointer to constant member function:
void (A::* const cpf)() const;

Constant pointer to non-constant member function:
void (A::* const cpf)();

In the first example the const is not on the left of *.

Fraser.
Jul 22 '05 #5

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

Similar topics

37
4943
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
4
4615
by: Dave | last post by:
Hello all, For purposes of understanding the language better, I tried to determine the offset of a struct member by using pointers to members (instead of something such as the offsetof macro). ...
110
9809
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
18
1586
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root ==...
13
2966
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
17
3216
by: djcredo | last post by:
Hey all, I want to return a pointer to a struct. Here is what I'lm trying to do: struct Position{ int x; int y; }; Position* GraphicTag::getRenderCentre(){
6
9851
by: dtschoepe | last post by:
Hi all, Working on homework again... I've got a weird problem, I've been banging my head against the wall on what is causing it. I have a pointer to a typdef named Person. At one point in the...
6
1718
by: Adrian | last post by:
Example A fails to compile with exampleA.cpp: In member function 'void A::a() const': exampleA:10: error: no matching function for call to 'A::b(const std::string&) const' exampleA:17: note:...
20
12379
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
0
7223
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...
1
6899
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
5475
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,...
1
4919
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...
0
4602
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...
0
3103
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...
1
665
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.