473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Accessing private member functions

Hi ppl

I have a question about memory layout of a class. Consider the code
below:

class Base1 {
virtual void f() { cout << "Base1::f" << endl; }
virtual void g() { cout << "Base1::g" << endl; }
};
class Drive : public Base1{
public:
virtual void fd() { cout << "Drive::fd" << endl; }
virtual void gd() { cout << "Drive::gd" << endl; }
};
typedef void(*Fun)(void);
int main() {
Drive objDrive;
Fun pFun = NULL;

int *temp1=(int*)&objDrive+0;
int *temp2=(int*)(*temp1);
pFun=(Fun)((int*)*(temp2+0));
pFun();

temp1=(int*)&objDrive+0;
temp2=(int*)(*temp1);
int *temp3 = (int*)*(temp2+1);
pFun=(Fun)temp3;
pFun();

return 0;
}
This outputs:

Base1::f
Base1::g

This is understandable because of the VPTR and VTable being created
when the derived class object is made. Note that though the virtual
function was private to base class, it can still be called from the
derived class object. Now my question is:

What happens if the base class functions were not virtual but normal
functions? Is there any way we can access the function pointers of
these private member functions from the derived class object?? What is
the memory layout of the derived class object that would be created in
this case?

Thanks in advance
Harry

Nov 10 '05 #1
6 2582
harry wrote:
Hi ppl

I have a question about memory layout of a class. Consider the code
below:

class Base1 {
virtual void f() { cout << "Base1::f" << endl; }
virtual void g() { cout << "Base1::g" << endl; }
};
class Drive : public Base1{
public:
virtual void fd() { cout << "Drive::fd" << endl; }
virtual void gd() { cout << "Drive::gd" << endl; }
};
typedef void(*Fun)(void);
int main() {
Drive objDrive;
Fun pFun = NULL;

int *temp1=(int*)&objDrive+0;
int *temp2=(int*)(*temp1);
pFun=(Fun)((int*)*(temp2+0));
pFun();

temp1=(int*)&objDrive+0;
temp2=(int*)(*temp1);
int *temp3 = (int*)*(temp2+1);
pFun=(Fun)temp3;
pFun();

return 0;
}
This outputs:

Base1::f
Base1::g

This is understandable because of the VPTR and VTable being created
when the derived class object is made. Note that though the virtual
function was private to base class, it can still be called from the
derived class object. Now my question is:

What happens if the base class functions were not virtual but normal
functions? Is there any way we can access the function pointers of
these private member functions from the derived class object?? What is
the memory layout of the derived class object that would be created in
this case?

Thanks in advance
Harry


If I saw this in production code and I were your manager, I would
probably have you fired (that is, terminate your employment, not set
you ablaze, although that's not such a bad idea in this case either).

First, you are dealing with a compiler-dependent operation when you try
to access the v-table manually, as noted in the FAQ (e.g.,
http://www.parashift.com/c++-faq-lit...html#faq-38.9).
Second, you are using C-style casts instead of C++ style. Third, you
are writing illegible code. Fourth, your class design is probably
flawed if you need to access private members of a base class. Expand
your interface or add a friend, but do not do what you have done here.

Cheers! --M

Nov 10 '05 #2
hehe...well...nice mail from a fellow coder(or r u??) who thinks
radically different....ok...i get the fact abt C-style casts but u have
to pardon me for that cos i am a C coder and not a C++ coder. I
disagree with the other 2 points because:

1. I work only with MS compilers and do not have to make products that
would be running in all compilers. And what I have written
is true for MS compilers only. I dont really care abt the other
ones. Plain and Simple.
2. Illegible code?? Illegible code would be if I write something like
this:
pFun = (Fun)*((int*)*(int*)((int*)&objDrive+0)+0);
pFun();
It works fine but I consider this illegible. What I have written is
purely legible. You may have different standards. No comments
henceforth.
3. Well, class design is in front of u. I made a very simple class with
inheritance. If the compiler creates code that lets the developer
access private members (virtual functions. You cannot access any
private data members) thru VTables, I cannot really help it.

Lastly, I would never use such a code for any real time project. But I
dont know whether u have an idea but the whole COM arch is based on
virtual functions and VTables + templates + RPC (for out of proc
servers). I am trying to get under the hood to see how I can juice the
COM arch to the fullest. Given the fact that I am a C coder
(particularly with Windows kernel), I would never venture into COM but
there is still a BIG IF! Hence my quest for knowledge.

Anyway, I would appreciate if someone can give me an idea abt non
virtual members of the base class. It ought to have a function pointer
but can I get hold of it somehow? Is it possible? If neone has tried it
b4 (for fun sake) on ne C++ compiler that exists in this world, I would
be very glad if he/she can share the info.
No hard feelings --M

regards
Harry

Nov 10 '05 #3
harry wrote:
hehe...well...nice mail from a fellow coder(or r u??) who thinks
radically different....ok...i get the fact abt C-style casts but u have
to pardon me for that cos i am a C coder and not a C++ coder. I
disagree with the other 2 points because:

1. I work only with MS compilers and do not have to make products that
would be running in all compilers. And what I have written
is true for MS compilers only. I dont really care abt the other
ones. Plain and Simple.


Then you probably want an MS compiler newsgroup, perhaps one suggested
in

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

since this newsgroup doesn't care about compiler-specific behaviour.

Gavin Deane

Nov 10 '05 #4
Please quote the message you are responding to and place your response
inline or below it. It helps others who are not using Google Groups to
follow the discussion more easily. (To quote a message from Google,
click on "show options" and then click on "Reply" in the revealed
header.)

harry wrote:
hehe...well...nice mail from a fellow coder(or r u??)
I am.
who thinks
radically different....ok...i get the fact abt C-style casts but u have
to pardon me for that cos i am a C coder and not a C++ coder. I
disagree with the other 2 points because:

1. I work only with MS compilers and do not have to make products that
would be running in all compilers. And what I have written
is true for MS compilers only. I dont really care abt the other
ones. Plain and Simple.
That's fine, but this newsgroup is for purely *standard* C++. You're
really interested in the Microsoft implementation of virtual tables and
should take your question to microsoft.public.vc.language or some such
forum where they can provide you non-standard advice.
2. Illegible code?? Illegible code would be if I write something like
this:
pFun = (Fun)*((int*)*(int*)((int*)&objDrive+0)+0);
pFun();
It works fine but I consider this illegible. What I have written is
purely legible. You may have different standards. No comments
henceforth.
It is a subjective thing, but I don't think many here would disagree
with my assessment.
3. Well, class design is in front of u. I made a very simple class with
inheritance. If the compiler creates code that lets the developer
access private members (virtual functions. You cannot access any
private data members) thru VTables, I cannot really help it.
But my point stands. You're fighting against the language here. Private
members are intended to be private for a reason, and if you really must
exploit tricks to get around the compiler's facilities for good design
(e.g., private members), then I can only conclude that your design is
flawed and should be changed.
Lastly, I would never use such a code for any real time project.
I never said "real time".
But I
dont know whether u have an idea but the whole COM arch is based on
virtual functions and VTables + templates + RPC (for out of proc
servers).
I do have an idea. :-)
I am trying to get under the hood to see how I can juice the
COM arch to the fullest. Given the fact that I am a C coder
(particularly with Windows kernel), I would never venture into COM but
there is still a BIG IF! Hence my quest for knowledge.
"BIG IF". I'm not familiar with the acronym. ;-)
Anyway, I would appreciate if someone can give me an idea abt non
virtual members of the base class. It ought to have a function pointer
but can I get hold of it somehow? Is it possible? If neone has tried it
b4 (for fun sake) on ne C++ compiler that exists in this world, I would
be very glad if he/she can share the info.
The compiler doesn't store function pointers for non-virtual functions.
Rather it converts calls to member functions to ordinary functions.
E.g.,

struct Foo { void Bar( int ); };
Foo foo;
foo->Bar( 42 );

.... becomes something like:

Bar__Foo( foo, 42 );

Thus, you can see that it doesn't need to store pointers to non-virtual
functions since they are really just ordinary functions no different
than, say, std::printf.

That being said, you can get a pointer to a member function quite
simply. Check the FAQ for the details:

http://www.parashift.com/c++-faq-lit...o-members.html
No hard feelings --M
Ditto.
regards
Harry


Cheers! --M

Nov 10 '05 #5
mlimber wrote:
[snip]
struct Foo { void Bar( int ); };
Foo foo;
foo->Bar( 42 );
That should be:
foo.Bar( 42 );

... becomes something like:

Bar__Foo( foo, 42 );


And that should be:
Bar__Foo( &foo, 42 );

[snip]

Cheers! --M

Nov 10 '05 #6
Hi

Hope this message would be more readable. Thanks again for the
mail...got the answer. And next time my mail would be on a MS group. I
think that is more suitable to me. Thanks neway

regards
Harry
mlimber wrote:
Please quote the message you are responding to and place your response
inline or below it. It helps others who are not using Google Groups to
follow the discussion more easily. (To quote a message from Google,
click on "show options" and then click on "Reply" in the revealed
header.)

harry wrote:
hehe...well...nice mail from a fellow coder(or r u??)


I am.
who thinks
radically different....ok...i get the fact abt C-style casts but u have
to pardon me for that cos i am a C coder and not a C++ coder. I
disagree with the other 2 points because:

1. I work only with MS compilers and do not have to make products that
would be running in all compilers. And what I have written
is true for MS compilers only. I dont really care abt the other
ones. Plain and Simple.


That's fine, but this newsgroup is for purely *standard* C++. You're
really interested in the Microsoft implementation of virtual tables and
should take your question to microsoft.public.vc.language or some such
forum where they can provide you non-standard advice.
2. Illegible code?? Illegible code would be if I write something like
this:
pFun = (Fun)*((int*)*(int*)((int*)&objDrive+0)+0);
pFun();
It works fine but I consider this illegible. What I have written is
purely legible. You may have different standards. No comments
henceforth.


It is a subjective thing, but I don't think many here would disagree
with my assessment.
3. Well, class design is in front of u. I made a very simple class with
inheritance. If the compiler creates code that lets the developer
access private members (virtual functions. You cannot access any
private data members) thru VTables, I cannot really help it.


But my point stands. You're fighting against the language here. Private
members are intended to be private for a reason, and if you really must
exploit tricks to get around the compiler's facilities for good design
(e.g., private members), then I can only conclude that your design is
flawed and should be changed.
Lastly, I would never use such a code for any real time project.


I never said "real time".
But I
dont know whether u have an idea but the whole COM arch is based on
virtual functions and VTables + templates + RPC (for out of proc
servers).


I do have an idea. :-)
I am trying to get under the hood to see how I can juice the
COM arch to the fullest. Given the fact that I am a C coder
(particularly with Windows kernel), I would never venture into COM but
there is still a BIG IF! Hence my quest for knowledge.


"BIG IF". I'm not familiar with the acronym. ;-)
Anyway, I would appreciate if someone can give me an idea abt non
virtual members of the base class. It ought to have a function pointer
but can I get hold of it somehow? Is it possible? If neone has tried it
b4 (for fun sake) on ne C++ compiler that exists in this world, I would
be very glad if he/she can share the info.


The compiler doesn't store function pointers for non-virtual functions.
Rather it converts calls to member functions to ordinary functions.
E.g.,

struct Foo { void Bar( int ); };
Foo foo;
foo->Bar( 42 );

... becomes something like:

Bar__Foo( foo, 42 );

Thus, you can see that it doesn't need to store pointers to non-virtual
functions since they are really just ordinary functions no different
than, say, std::printf.

That being said, you can get a pointer to a member function quite
simply. Check the FAQ for the details:

http://www.parashift.com/c++-faq-lit...o-members.html
No hard feelings --M


Ditto.
regards
Harry


Cheers! --M


Nov 10 '05 #7

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

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
6
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >>...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
2
by: mickey22 | last post by:
Hi all, I have some data members and member functions that are declared as private in a class A. Now I want to use these data members and functions(which are private) in my new C++ source...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
1
by: vinothg | last post by:
Guys, I just wanna know how compiler diffrentiates between a ordinary function and member function. I know that compiler will secretly pass the this pointer to member functions. Eg : look at...
3
by: jthep | last post by:
I have been given the class definition below: class llist { private: record *start; char filename; int readfile(); int writefile(); record * reverse(record *);
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.