printing the address of a non static member function of a clss
-
hi everyone im trying to check if my compiler creates a copy of the nonstatic member functions for each object or it just shares the code. to do that i wrote this code: /******************************* #include using namespace std; class cls { public: int f( ) { //cout << (void*)(f) <
-
hi everyone im trying to check if my compiler creates a copy of the nonstatic member functions for each object or it just shares the code. to do that i wrote this code: /******************************* #include using namespace std; class cls { public: int f( ) { //cout << (void*)(f) <
firstly, you can be sure that the compiler shares only one copy of the member functions body. but you could do this to test :
class cls {
public;
void f() {
}
};void main() {
cls a, b;
printf("%l %l", (void*)(&(a.f)), (void*)(&(b.f)));
}-- modified at 13:19 Monday 6th March, 2006 (thanks Mike)
-
firstly, you can be sure that the compiler shares only one copy of the member functions body. but you could do this to test :
class cls {
public;
void f() {
}
};void main() {
cls a, b;
printf("%l %l", (void*)(&(a.f)), (void*)(&(b.f)));
}-- modified at 13:19 Monday 6th March, 2006 (thanks Mike)
the code u gave me didnt work!! another thing if the compiler always shares the space why would static functions exist in the first place???!!!
-
the code u gave me didnt work!! another thing if the compiler always shares the space why would static functions exist in the first place???!!!
Amr Shahin wrote:
the code u gave me didnt work!!
why ?? any error ?
Amr Shahin wrote:
if the compiler always shares the space why would static functions exist in the first place???!!!
the difference between static/non-static member function are for the implicit
this
parameter. generally, a member function knows on which object it works on, because it is getting an implicit parameter, which is in C++ thethis
pointer. i say generally because static member function don't ! they can only perform general task, and can only access static members... but no way this has to relates with body copies. a function body is written once in the source code, do why would it be loaded multiple times in memory. the only difference between the calls are the parameters the function receives, but this is a stack problem... -
hi everyone im trying to check if my compiler creates a copy of the nonstatic member functions for each object or it just shares the code. to do that i wrote this code: /******************************* #include using namespace std; class cls { public: int f( ) { //cout << (void*)(f) <
Amr Shahin wrote:
cout << (void*)f <<endl;
Change that to:
cout << (void*) &cls::f <<endl;
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Amr Shahin wrote:
cout << (void*)f <<endl;
Change that to:
cout << (void*) &cls::f <<endl;
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
i tried ur code and it gave me the following error: /*********************** test.cpp:13: error: invalid use of non-static member function ‘void cls::f()’ test.cpp:13: error: converting from ‘void (cls::)()’ to ‘void ****************************!!!/ any ideas ??