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::som eFunc 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(w hich 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 11 1525
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(w hich 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
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
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::som eFunc 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(w hich 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
"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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
"Ian" <ia******@hotma il.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::som eFunc 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(w hich 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
Jon Slaughter wrote: "Ian" <ia******@hotma il.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
"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. . Jon Slaughter wrote: "Ian" <ia******@hotma il.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
"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. . Jon Slaughter wrote: "Ian" <ia******@hotma il.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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
-------------------------------------------------------------
PEP: 323
|
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 important
changes, so in this thread I would like to focus on that issue alone.
I have edited the Proposed Syntax example below to take out the
changes unecessary to this discussion. I left in the change of
"instance variable" syntax (...
|
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 maybe even just declare an interface, with
no methods implemented). Right now, you don't really have a way to do
it. You can leave the methods with a "pass", or raise a
NotImplementedError, but even in the best solution that I know of,
|
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 be dispatched to the
corresponding child class.
Somewhat silly example:
class Address:
def __init__():
self.displayed_name = ''
|
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 words, defined a class class_A I'd like to be able to do the
following:
// defining an array of class_A objects
class_A myArray = new class_A;
| |
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 stable (so I can
regenerate it later if necessary).
I thought the id function was the obvious choice, but it doesn't seem to
work. The id of two different methods of the same object seems to be the
same, and it may not be stable either. For...
|
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.
Foo = function(type) {
this.num = 0;
this.type = type
this.trigger();
}
Foo.prototype.trigger = function() {
|
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 esoteric uses (abuses?) of the language.
I've been working from the oft quoted resource
http://www.crockford.com/javascript/private.html. During my first
serious attempt at using the knowledge acquired from this page, I ran
up against the problem...
|
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
|
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly.
In this article I'll take a look at the equals and hashCode methods.
...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |