printing an address of member function

S

salmonella

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){};
};

void (MyClass::*ptr)(void);

void main(void)
{
MyClass myObject;
ptr = MyClass::fun;
cout << myObject.*ptr << endl;

// ...
}

An error occures while compilation:

"fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp',
line 2701) Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information"

My questions are:

1. Why I got this error?
2. How should I do it (print address) properly?

TIA
 
B

Bob Hairgrove

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){};
};

void (MyClass::*ptr)(void);

void main(void)

This should be:
int main()
// or:
int main(void)

Returning void from main invokes undefined behavior in C++.
{
MyClass myObject;
ptr = MyClass::fun;

Should be:
ptr = &MyClass::fun;
cout << myObject.*ptr << endl;

Should be:
cout << ptr << endl;
// ...
}

An error occures while compilation:

"fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp',
line 2701) Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information"

My questions are:

1. Why I got this error?

Because you have an old compiler which is hopelessly broken in many
ways. If you had a decent compiler, you would get different errors,
but not "INTERNAL COMPILER ERROR".
2. How should I do it (print address) properly?

Try this instead:

#include <iostream>
#include <ostream>

class MyClass
{
public:
void fun(void){};
};

void (MyClass::*ptr)(void);

using namespace std;

int main()
{
MyClass myObject;
ptr = &MyClass::fun;
cout << ptr << endl;
return 0;
}
 
S

salmonella

Bob said:
This should be:
int main()
// or:
int main(void)

Returning void from main invokes undefined behavior in C++.
You are right. It is not a good practice (even in such a simple code)
Should be:
ptr = &MyClass::fun;
IMHO both methods are OK. I think the name of the function is it's
address at the same time.
Should be:
cout << ptr << endl;
It doesn't show the address of the function - it allways output "1"
(that is the problem)
Because you have an old compiler which is hopelessly broken in many
ways. If you had a decent compiler, you would get different errors,
but not "INTERNAL COMPILER ERROR".
I forgot to writa that I use VC++ 7.1
Try this instead:

#include <iostream>
#include <ostream>

class MyClass
{
public:
void fun(void){};
};

void (MyClass::*ptr)(void);

using namespace std;

int main()
{
MyClass myObject;
ptr = &MyClass::fun;
cout << ptr << endl;
return 0;
}
It always prints "1" on screen (i guess its not the address)

Thanks anyway.
 
S

salmonella

Roland said:

Thanks,

this expleins a little by telling about difference between pointers to
"normal" functions and pointers to member functions.

That's why I shouldn't attempt to "cast" a pointer-to-member-function
into a pointer-to-function.

I still wonder if there is any possibility to get to know a "real"
address of function (because it must have an address when it is called
for a certain object).
 
B

Bob Hairgrove

You are right. It is not a good practice (even in such a simple code)

"Bad practice", however, can also be code that does not invoke
undefined behavior, but (for whatever reason) is used from time to
time anyway. Undefined behavior is something which should always be
avoided.
IMHO both methods are OK. I think the name of the function is it's
address at the same time.

This is true for static member functions as well as stand-alone
functions. For non-static member functions, section 5.3.1 paragraph 3
of the C++ standard seems to require the "&". But it doesn't surprise
me that MSVC lets you get away with it.
It doesn't show the address of the function - it allways output "1"
(that is the problem)

That's because pointers to members are a kind of offset into the class
structure, not an absolute address. And there is only one instance of
a member function per class -- not per object. That is why it is
impossible to get a real address for such a function without resorting
to platform-specific tricks (e.g. using inline assembly to read the
value of the stack register).
 
K

Kai-Uwe Bux

Bob said:
[snip]
void main(void)

This should be:
int main()
// or:
int main(void)

Returning void from main invokes undefined behavior in C++.

From the standard [3.6.1/2]:

An implementation shall not predefine the main function. This function
shall not be overloaded. It shall have a return type of type int, but
otherwise its type is implementation-defined.

I think the phrase "shall have a return type of int" says that a signature
returning any type other than int from main is simply in violation of
diagnosable rules and that diagnostics is required per [1.4/1]. I see no
undefined behavior here.


[snip]


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top