473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Methods of Objects

Are methods of a class created only once for all objects or for each object
as its own copy of its methods?

It seems that I can only access an object method's address using :: so if I
have a class with method func and I want its address I would do
&someClass::someFunc which seems to imply that there is only one function
per class(and hence for all instantiations of that class)... when I create a
very large array of objects of that class it seems to back this up as the
file size does not change much...

I just want to be sure that there is no need to deal with static members and
stuff for no to make sure I optimize size if the compiler handles it
automatically(which I hope it does)? i.e. there is virtually no difference
between these two classes
class A
{
int x;
public:
void func(int y) { x = y; }
}

and

class B
{
int x;
public:
static void func(B &b, int y) { b.x = y; }
}
a.func(1);

and

b.func(b, 1);

should produce the exact same code? (or very very close?) I'm hoping that
the compiler internally handles class A as class B or maybe even better. The
reason is that simply class B has only 1 func even if I do something like B
b[10000]; while I'm not sure about class A.. if I do A a[10000] if it
creates 10000 copies of func or not. Though I don't see any reason why this
would happen and all my "tests" say it doesn't... I just want to make sure
so.

Thanks,
Jon
Sep 5 '05 #1
11 1505
Jon Slaughter wrote:
Are methods of a class created only once for all objects or for each object
as its own copy of its methods?
The former.

[snip]
I just want to be sure that there is no need to deal with static members and
stuff for no to make sure I optimize size if the compiler handles it
automatically(which I hope it does)? i.e. there is virtually no difference
between these two classes
class A
{
int x;
public:
void func(int y) { x = y; }
}

and

class B
{
int x;
public:
static void func(B &b, int y) { b.x = y; }
}
The above implies sizeof(A) == sizeof(B) == sizeof(int). I.e., each
class should be the size of an int.
a.func(1);

and

b.func(b, 1);

should produce the exact same code? (or very very close?)
I expect they would be quite close, but it doesn't matter - the number
of copies of the member function does not increase as objects are
constructed.
I'm hoping that
the compiler internally handles class A as class B or maybe even better. The
reason is that simply class B has only 1 func even if I do something like B
b[10000]; while I'm not sure about class A.. if I do A a[10000] if it
creates 10000 copies of func or not.


It doesn't.

Best regards,

Tom

Sep 6 '05 #2

Thomas Tutone wrote:
Jon Slaughter wrote:
Are methods of a class created only once for all objects or for each object
as its own copy of its methods?


The former.


I should qualify this, I suppose. The code for the class's member
functions does not affect the size of the class, because the code is
not data. However, if you define the member function inline (as you do
in your example), then if the compiler complies with your inlining
request, a new copy of that code is inserted every time you call that
member function. It still has nothing to do with the size of the
constructed object, though, and each object does not have "its own copy
of it" member functions.

Best regards,

Tom

Sep 6 '05 #3
Ian
Jon Slaughter wrote:
Are methods of a class created only once for all objects or for each object
as its own copy of its methods?
Once, the instance of the object is identified by the this pointer.
It seems that I can only access an object method's address using :: so if I
have a class with method func and I want its address I would do
&someClass::someFunc which seems to imply that there is only one function
per class(and hence for all instantiations of that class)... when I create a
very large array of objects of that class it seems to back this up as the
file size does not change much...

I just want to be sure that there is no need to deal with static members and
stuff for no to make sure I optimize size if the compiler handles it
automatically(which I hope it does)? i.e. there is virtually no difference
between these two classes

Note that a pointer to a member function is a different beast from a
pointer to a static member.

Ian
Sep 6 '05 #4

"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

Thomas Tutone wrote:
Jon Slaughter wrote:
> Are methods of a class created only once for all objects or for each
> object
> as its own copy of its methods?


The former.


I should qualify this, I suppose. The code for the class's member
functions does not affect the size of the class, because the code is
not data. However, if you define the member function inline (as you do
in your example), then if the compiler complies with your inlining
request, a new copy of that code is inserted every time you call that
member function. It still has nothing to do with the size of the
constructed object, though, and each object does not have "its own copy
of it" member functions.

Best regards,

Tom


Ok, as long as the compiler does its best to optimize code and data so that
there is not any wasted space then its ok. Obviously if its inline then one
would increase the code size(but this, I think, is irrespective of the
number of objects but the number of calls to the inline function?).

Just wanted to make sure that in general the number of objects created do
not increase the size proportional to the method code size(obviously it has
to do with the data). It seemed like a logical thing to do and my examples
were only giving the right outcome but i wasn't completely sure if I was
missing something since I wasn't doing a very thorough test.

Thanks,

Jon
Sep 6 '05 #5

"Ian" <ia******@hotmail.com> wrote in message
news:11***************@drone2-svc-skyt.qsi.net.nz...
Jon Slaughter wrote:
Are methods of a class created only once for all objects or for each
object as its own copy of its methods?
Once, the instance of the object is identified by the this pointer.
It seems that I can only access an object method's address using :: so if
I have a class with method func and I want its address I would do
&someClass::someFunc which seems to imply that there is only one function
per class(and hence for all instantiations of that class)... when I
create a very large array of objects of that class it seems to back this
up as the file size does not change much...

I just want to be sure that there is no need to deal with static members
and stuff for no to make sure I optimize size if the compiler handles it
automatically(which I hope it does)? i.e. there is virtually no
difference between these two classes

Note that a pointer to a member function is a different beast from a
pointer to a static member.


how so? I thought static functions were identical to global functions as
far as the internal representation was concerned? If there is only one
function, wether static or not, per class then what is the difference?
Ian


Thanks,

Jon
Sep 6 '05 #6

Jon Slaughter wrote:
"Ian" <ia******@hotmail.com> wrote in message
Note that a pointer to a member function is a different beast from a
pointer to a static member.

how so? I thought static functions were identical to global functions as
far as the internal representation was concerned?


That's true.
If there is only one
function, wether static or not, per class then what is the difference?


I think Ian means that they involve two different kinds of _pointers_.
Take a look at Section 33 of the FAQ to see the difference:

http://www.parashift.com/c++-faq-lit...o-members.html

Best regards,

Tom

Sep 6 '05 #7

"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Jon Slaughter wrote:
"Ian" <ia******@hotmail.com> wrote in message
> Note that a pointer to a member function is a different beast from a
> pointer to a static member.
>
how so? I thought static functions were identical to global functions as
far as the internal representation was concerned?


That's true.
If there is only one
function, wether static or not, per class then what is the difference?


I think Ian means that they involve two different kinds of _pointers_.
Take a look at Section 33 of the FAQ to see the difference:

http://www.parashift.com/c++-faq-lit...o-members.html


You mean the
a.. Its type is "int (*)(char,float)" if an ordinary function
b.. Its type is "int (Fred::*)(char,float)" if a non-static member
function of class Fred

part?

Seems to me that this only addresses the resolution of the function but
internally they are the same? (just pointers) Its up to the compiler to
determine how those pointers function and there arguments to be placed on
the stack properly, etc...

I mean, as far as I can see its just saying that if I wanted to create a
pointer to a member function I have to use that syntax so the compiler will
"know" but in all actuality its still just a pointer function that the
resolution is just some "meta syntax" that exists in the language to make it
easier to program and keep things encapsulated(i.e. I could write everytin
in C that would produce the "exact" same code that doesn't use classes or
anything but the classes are really just a way to group data and code to
make programming easier but ultimately it pretty much represents the same
machine code in the long run?)?

Best regards,

Tom


Jon
Sep 6 '05 #8

"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Jon Slaughter wrote:
"Ian" <ia******@hotmail.com> wrote in message
> Note that a pointer to a member function is a different beast from a
> pointer to a static member.
>
how so? I thought static functions were identical to global functions as
far as the internal representation was concerned?


That's true.
If there is only one
function, wether static or not, per class then what is the difference?


I think Ian means that they involve two different kinds of _pointers_.
Take a look at Section 33 of the FAQ to see the difference:

http://www.parashift.com/c++-faq-lit...o-members.html


You mean the
a.. Its type is "int (*)(char,float)" if an ordinary function
b.. Its type is "int (Fred::*)(char,float)" if a non-static member
function of class Fred

part?

Seems to me that this only addresses the resolution of the function but
internally they are the same? (just pointers) Its up to the compiler to
determine how those pointers function and there arguments to be placed on
the stack properly, etc...

I mean, as far as I can see its just saying that if I wanted to create a
pointer to a member function I have to use that syntax so the compiler will
"know" but in all actuality its still just a pointer function that the
resolution is just some "meta syntax" that exists in the language to make it
easier to program and keep things encapsulated(i.e. I could write everytin
in C that would produce the "exact" same code that doesn't use classes or
anything but the classes are really just a way to group data and code to
make programming easier but ultimately it pretty much represents the same
machine code in the long run?)?

Best regards,

Tom


Jon
Sep 6 '05 #9
Ian
Jon Slaughter wrote:

You mean the
a.. Its type is "int (*)(char,float)" if an ordinary function
b.. Its type is "int (Fred::*)(char,float)" if a non-static member
function of class Fred

part?

Seems to me that this only addresses the resolution of the function but
internally they are the same? (just pointers) Its up to the compiler to
determine how those pointers function and there arguments to be placed on
the stack properly, etc...

No, they are not. That's what I was trying to say before.

Ian
Sep 6 '05 #10
Jon Slaughter wrote:
[...] Obviously if its inline then one
would increase the code size(but this, I think, is irrespective of the
number of objects but the number of calls to the inline function?).


That's right, there's no connection whatsoever between the number of
class /instances/ and the quantity of code produced for operating on
those instances. This code is generated once, for the class itself.
Instances of a class only store state information.
Sep 6 '05 #11
Jon Slaughter wrote:

I mean, as far as I can see its just saying that if I wanted to create a
pointer to a member function I have to use that syntax so the compiler will
"know" but in all actuality its still just a pointer function that the
resolution is just some "meta syntax" that exists in the language to make it
easier to program and keep things encapsulated(i.e. I could write everytin
in C that would produce the "exact" same code that doesn't use classes or
anything but the classes are really just a way to group data and code to
make programming easier but ultimately it pretty much represents the same
machine code in the long run?)?


You are missing that somehow a member function must be able to identify the
object it is working on. A static function doesn't need to do that, since
it is independent of any object.

So how does a member function identify the object it is working on?
Most compilers do it this way:
They pass a hidden parameter to the function: a pointer to the object.
This pointer becomes the 'this' pointer inside the member function.

So when you do:

class X
{
public:
void foo();

private:
int x;
};

void X::foo()
{
x = 5;
}

then the compiler internally comes up with:

struct X
{
int x;
};

void X_foo( X* pThis )
{
pThis->x = 5;
}

and changes a call to the member function:

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

to:

int main()
{
X Obj;

X_foo( &Obj );
}

This should also answer your question if every object needs a seperate
code base. Obviously not. Each function can exist just once. When the
function is called, the address of the object it should work on, is passed
to that function.

But now you can also see, that an ordinary member function cannot have the
same signature then a normal function or a static function. The object it
should work on, must be passed to that function.

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 6 '05 #12

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

Similar topics

35
3701
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
99
5818
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
51
6881
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
1
1324
by: Dave Merrill | last post by:
Python newb here. Say a class contains some rich attributes, each defined as a class. If an instance of the parent class recieves a call to a method belonging to one of those attributes, it should...
7
1716
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other...
12
2678
by: Russell E. Owen | last post by:
I have several situations in my code where I want a unique identifier for a method of some object (I think this is called a bound method). I want this id to be both unique to that method and also...
12
5505
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
3
1761
by: rickeringill | last post by:
Hi comp.lang.javascript, I'm throwing this in for discussion. First up I don't claim to be any sort of authority on the ecmascript language spec - in fact I'm a relative newb to these more...
26
2500
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
10
104982
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
7220
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
7371
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...
1
7023
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
7479
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
5617
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
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
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...
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
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...

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.