473,396 Members | 1,853 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,396 software developers and data experts.

How are functions organized in c++

Hi Everyone,

I have this following class

class xx{
public:
int func(){}
};

I have a main as follows

int main(){
xx x1,x2;
x1.func();
x2.func();
}

My questions ?
============

How does the c++ compiler organise the functions inside a class ?.

Are they stored as function pointers something like this
int (xx::func) () something the user cannot figure out and to him/her
it appears as object.member_function() e.g x1.func()

While we were discussing all this some one told me that x1.func()
actually resolves to x1.func(*this)

Still another guy was of the opinion that x1.func() resolves to
x.func(&x1).

All this provides different thoughts and different interpretations

Please provide some clarity on how the compiler would actually organise
it

My thanks in advance

Thanks
Ranjaya

Aug 23 '06 #1
8 1243
Ranjay wrote:
Hi Everyone,

I have this following class

class xx{
public:
int func(){}
};

I have a main as follows

int main(){
xx x1,x2;
x1.func();
x2.func();
}

My questions ?
============

How does the c++ compiler organise the functions inside a class ?.
That depends on the compiler.
Are they stored as function pointers something like this
int (xx::func) () something the user cannot figure out and to him/her
it appears as object.member_function() e.g x1.func()
No. Usually, they are internally just implemented like nonmember functions
that get as a hidden extra parameter a pointer to the object they were
called for (the 'this' pointer).
While we were discussing all this some one told me that x1.func()
actually resolves to x1.func(*this)

Still another guy was of the opinion that x1.func() resolves to
x.func(&x1).
Basically, out of

struct X
{
void foo() { i = 5;}
int i;
};

int main()
{
X x;
x.foo();
}

the compiler makes something like:

struct X
{
int i;
}

void __X_foo(X* this)
{
this->i = 5;
}

int main()
{
X x;
__X_foo(x);
}
>
All this provides different thoughts and different interpretations

Please provide some clarity on how the compiler would actually organise
it
Why does it matter?

Aug 23 '06 #2
Rolf Magnus wrote:
Ranjay wrote:
>Hi Everyone,
[...]
Please provide some clarity on how the compiler would actually
organise it

Why does it matter?
Hey, he wants to know, why should we care why he wants to know?

Ranjay, get yourself a copy of "Inside the C++ Object Model".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '06 #3
Victor Bazarov wrote:
Rolf Magnus wrote:
>Ranjay wrote:
>>Hi Everyone,
[...]
Please provide some clarity on how the compiler would actually
organise it

Why does it matter?

Hey, he wants to know, why should we care why he wants to know?
Well, I want to know why he wants to know. Anything wrong with that?

Aug 23 '06 #4
Rolf Magnus wrote:
Victor Bazarov wrote:
>Rolf Magnus wrote:
>>Ranjay wrote:
Hi Everyone,
[...]
Please provide some clarity on how the compiler would actually
organise it

Why does it matter?

Hey, he wants to know, why should we care why he wants to know?

Well, I want to know why he wants to know. Anything wrong with that?
Nothing, unless it's intended as a put-off, or as a rhetorical question
meaning "you don't need to know".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '06 #5
Victor Bazarov wrote:
Rolf Magnus wrote:
>Victor Bazarov wrote:
>>Rolf Magnus wrote:
Ranjay wrote:
Hi Everyone,
[...]
Please provide some clarity on how the compiler would actually
organise it

Why does it matter?

Hey, he wants to know, why should we care why he wants to know?

Well, I want to know why he wants to know. Anything wrong with that?

Nothing, unless it's intended as a put-off, or as a rhetorical question
meaning "you don't need to know".
In that case, I wouldn't have answered the OP's question first.

Aug 23 '06 #6

Rolf Magnus wrote:
Ranjay wrote:
[snip]
How does the c++ compiler organise the functions inside a class ?.

That depends on the compiler.
Are they stored as function pointers something like this
int (xx::func) () something the user cannot figure out and to him/her
it appears as object.member_function() e.g x1.func()

No. Usually, they are internally just implemented like nonmember functions
that get as a hidden extra parameter a pointer to the object they were
called for (the 'this' pointer).
[snip]

All this provides different thoughts and different interpretations

Please provide some clarity on how the compiler would actually organise
it

Why does it matter?
Well.... if the original poster thought that every member function took
up space in each object defined, he (and the rest of us!) would have a
very good reason to avoid member functions.

I had a collegue once who suffered from this misconception - and he
avoided memberfunctions like the plague, with a not so nice result. (He
was an old C-programmer so it did surprise me somewhat that he could
believe so), so even if that thought could look silly at first let's
not judge to hastily.

/Peter

Aug 23 '06 #7
Rolf Magnus wrote:
Victor Bazarov wrote:
>Rolf Magnus wrote:
>>Victor Bazarov wrote:

Rolf Magnus wrote:
Ranjay wrote:
>Hi Everyone,
>[...]
>Please provide some clarity on how the compiler would actually
>organise it
>
Why does it matter?

Hey, he wants to know, why should we care why he wants to know?

Well, I want to know why he wants to know. Anything wrong with that?

Nothing, unless it's intended as a put-off, or as a rhetorical
question meaning "you don't need to know".

In that case, I wouldn't have answered the OP's question first.
Good defensive move! Nobody should suspect nothin'. "Here is the
answer, but why in hell do you think you need to know?" Give him
the candy and then slap him, just so the life didn't seem too sweet.

Ah, forget it, my coffee hasn't kicked in yet, probably.
Aug 23 '06 #8
"Ranjay" <ra******@gmail.comwrites:
Are they stored as function pointers something like this
int (xx::func) () something the user cannot figure out and to him/her
it appears as object.member_function() e.g x1.func()
That's more or less how virtual methods work. Even though still
I suppose that instead of holding a pointer to each virtual method the
compiler puts a single pointer to a structure with pointers to virtual
methods (and each class has it's own such structure), so if you call
foo.virtualMethod() you are really calling something like
foo._virtual_methods->virtualMthod(&foo) where _virtual_methods points
to some magical structure _Foo_virtual_methods with all the pointers.

It of course may work differently plus it probably gets more
complicated when multiple inheritance comes in place.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Aug 23 '06 #9

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

Similar topics

2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
1
by: PTS | last post by:
I am working on a program that will calculate some numbers. As an example, I will say student average test score and print out the grade but my question is how do I put AVERAGE and test score and...
10
by: Jean | last post by:
Hi, how to know for a particular table if it is organized by index, and which one ? reorg table index ind1 -> the table is organised by ind1 i wan't to know that for all table thx
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
4
by: junky_fellow | last post by:
what is the purpose of using library functions ? How could i know whether a particular function that i want to write already exists in the library ? Are there some standard functions which...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
13
by: Simon Dean | last post by:
Hi, I have a couple of questions. If you don't mind. Sorry, I do get a bit wordy at times. First one just throws some thoughts around hoping for answers :-) 1) Anyone got a theory on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.