Connecting Tech Pros Worldwide Help | Site Map

printing an address of member function

salmonella
Guest
 
Posts: n/a
#1: Mar 25 '06
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
Bob Hairgrove
Guest
 
Posts: n/a
#2: Mar 25 '06

re: printing an address of member function


On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <asd@asd.com> wrote:
[color=blue]
>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)[/color]

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

Returning void from main invokes undefined behavior in C++.
[color=blue]
>{
> MyClass myObject;
> ptr = MyClass::fun;[/color]

Should be:
ptr = &MyClass::fun;
[color=blue]
> cout << myObject.*ptr << endl;[/color]

Should be:
cout << ptr << endl;
[color=blue]
>
> // ...
>}
>
>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?[/color]

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".
[color=blue]
>2. How should I do it (print address) properly?[/color]

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

--
Bob Hairgrove
NoSpamPlease@Home.com
Roland Pibinger
Guest
 
Posts: n/a
#3: Mar 25 '06

re: printing an address of member function


On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <asd@asd.com> wrote:
[color=blue]
>My questions are:
>
>1. Why I got this error?
>2. How should I do it (print address) properly?[/color]

see:
http://www.parashift.com/c++-faq-lit....html#faq-33.4




salmonella
Guest
 
Posts: n/a
#4: Mar 25 '06

re: printing an address of member function


Bob Hairgrove wrote:[color=blue]
> On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <asd@asd.com> wrote:[color=green]
>>void main(void)[/color]
>
> This should be:
> int main()
> // or:
> int main(void)
>
> Returning void from main invokes undefined behavior in C++.
>[/color]
You are right. It is not a good practice (even in such a simple code)[color=blue][color=green]
>>{
>> MyClass myObject;
>> ptr = MyClass::fun;[/color]
>
> Should be:
> ptr = &MyClass::fun;
>[/color]
IMHO both methods are OK. I think the name of the function is it's
address at the same time.[color=blue]
>[color=green]
>> cout << myObject.*ptr << endl;[/color]
>
>
> Should be:
> cout << ptr << endl;
>[/color]
It doesn't show the address of the function - it allways output "1"
(that is the problem)[color=blue][color=green]
>>
>>1. Why I got this error?[/color]
>
> 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".
>[/color]
I forgot to writa that I use VC++ 7.1[color=blue]
>[color=green]
>>2. How should I do it (print address) properly?[/color]
>
>
> 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;
> }
>[/color]
It always prints "1" on screen (i guess its not the address)

Thanks anyway.
salmonella
Guest
 
Posts: n/a
#5: Mar 25 '06

re: printing an address of member function


Roland Pibinger wrote:
[color=blue][color=green]
>>My questions are:
>>
>>1. Why I got this error?
>>2. How should I do it (print address) properly?[/color]
>
> see:
> http://www.parashift.com/c++-faq-lit....html#faq-33.4
>[/color]

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).
Bob Hairgrove
Guest
 
Posts: n/a
#6: Mar 26 '06

re: printing an address of member function


On Sat, 25 Mar 2006 23:18:30 +0100, salmonella <asd@asd.com> wrote:
[color=blue]
>Bob Hairgrove wrote:[color=green]
>> On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <asd@asd.com> wrote:[color=darkred]
>>>void main(void)[/color]
>>
>> This should be:
>> int main()
>> // or:
>> int main(void)
>>
>> Returning void from main invokes undefined behavior in C++.
>>[/color]
>You are right. It is not a good practice (even in such a simple code)[/color]

"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.
[color=blue][color=green][color=darkred]
>>>{
>>> MyClass myObject;
>>> ptr = MyClass::fun;[/color]
>>
>> Should be:
>> ptr = &MyClass::fun;
>>[/color]
>IMHO both methods are OK. I think the name of the function is it's
>address at the same time.[/color]

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.
[color=blue][color=green][color=darkred]
>>> cout << myObject.*ptr << endl;[/color]
>>
>>
>> Should be:
>> cout << ptr << endl;
>>[/color]
>It doesn't show the address of the function - it allways output "1"
>(that is the problem)[/color]

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).

--
Bob Hairgrove
NoSpamPlease@Home.com
Kai-Uwe Bux
Guest
 
Posts: n/a
#7: Mar 26 '06

re: printing an address of member function


Bob Hairgrove wrote:
[color=blue]
> On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <asd@asd.com> wrote:
>[/color]
[snip][color=blue]
>[color=green]
>>void main(void)[/color]
>
> This should be:
> int main()
> // or:
> int main(void)
>
> Returning void from main invokes undefined behavior in C++.[/color]

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
Roland Pibinger
Guest
 
Posts: n/a
#8: Mar 26 '06

re: printing an address of member function


On Sat, 25 Mar 2006 23:40:56 +0100, salmonella <asd@asd.com> wrote:[color=blue]
>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).[/color]

Probably there is no "real" address for member function pointers, see:
http://linuxquality.sunsite.dk/articles/memberpointers/

Best wishes,
Roland Pibinger
Closed Thread