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

printing an address of member function

P: n/a
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
Mar 25 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <as*@asd.com> wrote:
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;
}

--
Bob Hairgrove
No**********@Home.com
Mar 25 '06 #2

P: n/a
On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <as*@asd.com> wrote:
My questions are:

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


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


Mar 25 '06 #3

P: n/a
Bob Hairgrove wrote:
On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <as*@asd.com> wrote:
void main(void)
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)
{
MyClass myObject;
ptr = MyClass::fun;


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.
cout << myObject.*ptr << endl;

Should be:
cout << ptr << endl;

It doesn't show the address of the function - it allways output "1"
(that is the problem)

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

I forgot to writa that I use VC++ 7.1
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;
}

It always prints "1" on screen (i guess its not the address)

Thanks anyway.
Mar 25 '06 #4

P: n/a
Roland Pibinger wrote:
My questions are:

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


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


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).
Mar 25 '06 #5

P: n/a
On Sat, 25 Mar 2006 23:18:30 +0100, salmonella <as*@asd.com> wrote:
Bob Hairgrove wrote:
On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <as*@asd.com> wrote:
void main(void)


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)


"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.
{
MyClass myObject;
ptr = MyClass::fun;


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.


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.
cout << myObject.*ptr << endl;

Should be:
cout << ptr << endl;

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

--
Bob Hairgrove
No**********@Home.com
Mar 26 '06 #6

P: n/a
Bob Hairgrove wrote:
On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <as*@asd.com> wrote:
[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
Mar 26 '06 #7

P: n/a
On Sat, 25 Mar 2006 23:40:56 +0100, salmonella <as*@asd.com> wrote:
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).


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

Best wishes,
Roland Pibinger
Mar 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.