By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

pointer to member function and pointer to constant member function

P: n/a
Are these pointers the same?

Fraser.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.