473,468 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

About the address of member function

class Test
{
double i;

public:
void fun(){}
void foo(void)
{
cout << "as";
}
};

int main()
{
Test t;
t.foo; // mean what?
Test::foo; // mean what?

cout << &Test::foo << endl; // output 1, why?
cout << &Test::fun << endl; // output 1, too, why?

return 0;
}

Jun 13 '06 #1
3 1475
Sharpdew wrote:
class Test
{
double i;

public:
void fun(){}
void foo(void)
{
cout << "as";
}
};

int main()
{
Test t;
t.foo; // mean what?
Test::foo; // mean what?
The last two expressions mean nothing. They are illegal. You can make a
call:

t.foo()

Or take the address of the function:

&Test::foo

cout << &Test::foo << endl; // output 1, why?
cout << &Test::fun << endl; // output 1, too, why?

return 0;
}


Pointer to members are not really memory address in the strict sense.
You can think of it as a relative address (to whatever instance of that
type.)

Therefore, pointer to member cannot be converted to a normal pointer,
say, void*. Therefore it is not outputted as a normal pointer.

If you are so interested in the bit sequence of these pseudo pointers
you can do a forced conversion using reinterpret_cast, like so:

int main()
{
typedef void (Test::*tf)(void);

tf p1 = &Test::foo;
tf p2 = &Test::fun;

cout << *reinterpret_cast<void**>(&p1) << endl;
cout << *reinterpret_cast<void**>(&p2) << endl;
}
Jun 13 '06 #2
I'm sure know all you said, but what I want to know is the semantics of
these style code, that is to say, how do the compiler deal with them?
benben 写道:
Sharpdew wrote:
class Test
{
double i;

public:
void fun(){}
void foo(void)
{
cout << "as";
}
};

int main()
{
Test t;
t.foo; // mean what?
Test::foo; // mean what?


The last two expressions mean nothing. They are illegal. You can make a
call:

t.foo()

Or take the address of the function:

&Test::foo

cout << &Test::foo << endl; // output 1, why?
cout << &Test::fun << endl; // output 1, too, why?

return 0;
}


Pointer to members are not really memory address in the strict sense.
You can think of it as a relative address (to whatever instance of that
type.)

Therefore, pointer to member cannot be converted to a normal pointer,
say, void*. Therefore it is not outputted as a normal pointer.

If you are so interested in the bit sequence of these pseudo pointers
you can do a forced conversion using reinterpret_cast, like so:

int main()
{
typedef void (Test::*tf)(void);

tf p1 = &Test::foo;
tf p2 = &Test::fun;

cout << *reinterpret_cast<void**>(&p1) << endl;
cout << *reinterpret_cast<void**>(&p2) << endl;
}


Jun 13 '06 #3
I'm sure to know all you said, but what I want to know is the semantics
of
these style code, that is to say, how does the compiler deal with them?

benben wrote:
Sharpdew wrote:
class Test
{
double i;

public:
void fun(){}
void foo(void)
{
cout << "as";
}
};

int main()
{
Test t;
t.foo; // mean what?
Test::foo; // mean what?


The last two expressions mean nothing. They are illegal. You can make a
call:

t.foo()

Or take the address of the function:

&Test::foo

cout << &Test::foo << endl; // output 1, why?
cout << &Test::fun << endl; // output 1, too, why?

return 0;
}


Pointer to members are not really memory address in the strict sense.
You can think of it as a relative address (to whatever instance of that
type.)

Therefore, pointer to member cannot be converted to a normal pointer,
say, void*. Therefore it is not outputted as a normal pointer.

If you are so interested in the bit sequence of these pseudo pointers
you can do a forced conversion using reinterpret_cast, like so:

int main()
{
typedef void (Test::*tf)(void);

tf p1 = &Test::foo;
tf p2 = &Test::fun;

cout << *reinterpret_cast<void**>(&p1) << endl;
cout << *reinterpret_cast<void**>(&p2) << endl;
}


Jun 13 '06 #4

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

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
7
by: Howard | last post by:
Hi, in one of the recent posts, I saw someone pass two variables of built-in type (int and double), by reference, to a member function of a class. That function then took the addresses of those...
7
by: salmonella | last post by:
Hi, I'm trying to print on screen an address of class member function (the function of certain object of course). My code looks like this: class MyClass { public: void fun(void){}; };
22
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other...
3
by: telnet2008 | last post by:
Hi,everyone: the questionly code: #include <iostream> using namespace std; class test { int x;
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
8
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: ...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
8
by: Daniel T. | last post by:
#include <cassert> class Foo { public: virtual void fnA() = 0; virtual void fnB() = 0; }; int main() { assert( &Foo::fnB );
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
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,...
1
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.