473,581 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_f unction() 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 1251
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_f unction() 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_f unction() 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_f unction() 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.virtualMeth od() you are really calling something like
foo._virtual_me thods->virtualMthod(& foo) where _virtual_method s points
to some magical structure _Foo_virtual_me thods 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*jab ber.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
3771
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently,...
1
380
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 the other parts, call the functions and where? How would I structure that program? Would I declare each function before the MAIN() function, then...
10
6399
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
5886
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 route. The problem being that I can't call these functions from the query designer in Access. I decided that I would try the route of declaring the...
4
1533
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 ought to be taken by library ? Can i write a portable code without using library functions ? Can i write my own function and use it even if that...
19
4236
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 char* formatter, ...); Then, I want to call it as so:
23
3983
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 who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some...
47
3851
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 say, C programs consist of variables to store the input and functions to manipulate them. This would make C object-oriented - how cool would that be?...
13
1714
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 usage of PHP Classes rather than an actual technical guide? I've seen loads of things that show how to put together a class, but without actually...
0
7876
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7804
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6563
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.