473,396 Members | 1,864 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.

class method representation

I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables (yes, unless
I define a variable to be common, but let us say I did not
do that).

My question is this: does each instance have it's own
copy of the non-static methods?

Maybe this is a dumb question, but what motivates it is
this:

Say I have a class with a really huge complicated method
and I will be instantiating many, many objects of this class.
If each instance gets it's own copy of the huge method code,
then I might save some memory by modifying the method to
be a function (eg not a class method) and have only one
copy of it around.
Jul 19 '05 #1
10 6436
emerth wrote:
I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables (yes, unless
I define a variable to be common, but let us say I did not
do that).

My question is this: does each instance have it's own
copy of the non-static methods?


Usually not. What happens is that every method is converted (by the
compiler) to a "normal" function that accepts the "this" pointer to the
object instance it manipulates.

For instance, in this example:

1 int i;
2 std::string s = ...;
3 char c = s.at(i);

the compiler will actually generate for line 3 something like

char c = std::string::at(&s, i);

Of course inlining a function complicates matters slightly, but even then,
you basically get a copy of the function per function call, not a copy of
the function per object.

But try it yourself; write two classes with identical data members and
different methods (in number and "size") and compare their sizes with the
sizeof() operator.
Jul 19 '05 #2
emerth wrote:
I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables
(yes, unless I define a variable to be common,
but let us say I did not do that).

My question is this: does each instance have
it's own copy of the non-static methods?

Maybe this is a dumb question
but what motivates it is this:

Say I have a class with a really huge complicated method
and I will be instantiating many, many objects of this class.
If each instance gets it's own copy of the huge method code,
then I might save some memory
by modifying the method to be a function (e.g. not a class method)
and have only one copy of it around.


Objects do *not* have, include or contain methods.
Not in C++ or any other object oriented programming language.
The methods belong to the class. If there are virtual functions,
the object will contain a pointer to an array of function pointers
called the virtual function table. The function pointers
point to the methods which belong to the same class as the object.

Jul 19 '05 #3
emerth <em****@hotmail.com> wrote in message
news:a9**************************@posting.google.c om...
I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables (yes, unless
I define a variable to be common, but let us say I did not
do that).

My question is this: does each instance have it's own
copy of the non-static methods?

Maybe this is a dumb question, but what motivates it is
this:

Say I have a class with a really huge complicated method
and I will be instantiating many, many objects of this class.
If each instance gets it's own copy of the huge method code,
then I might save some memory by modifying the method to
be a function (eg not a class method) and have only one
copy of it around.


For the purposes of the language rules, it's best to think of each object as
having its own copy of each non-static data and function member. In effect,
that's what you get. However, in a given executable file, the actual
implementation will almost certainly share the same code among all objects,
so you don't have to worry your code growing every time you create an
object.

DW

Jul 19 '05 #4
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...
emerth wrote:
I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables
(yes, unless I define a variable to be common,
but let us say I did not do that).

My question is this: does each instance have
it's own copy of the non-static methods?

Maybe this is a dumb question
but what motivates it is this:

Say I have a class with a really huge complicated method
and I will be instantiating many, many objects of this class.
If each instance gets it's own copy of the huge method code,
then I might save some memory
by modifying the method to be a function (e.g. not a class method)
and have only one copy of it around.
Objects do *not* have, include or contain methods.


Conceptually they do. If I say ellipse.Area(), I am asking _that_ ellipse
for its area.
Not in C++ or any other object oriented programming language.
It was a while ago, but I recall methods in Smalltalk being fully fledged
objects.
The methods belong to the class.


Then I can call Ellipse::Area() without an object, which I can't.

DW

Jul 19 '05 #5
David White wrote:
For the purposes of the language rules, it's best
It is *not* best. It is not true
and it leads to the kind of confusion expressed by emerth.
to think of each object as having
its own copy of each non-static data and function member.
No. This would imply that you could mix and match methods
from other classes freely which is *not* true.
The methods belong to the class of which the object is an instance.
No substitutions are allowed!
In effect, that's what you get.
However, in a given executable file, the actual implementation
will almost certainly share the same code among all objects,
so you don't have to worry about your code growing
every time you create an object.


Jul 19 '05 #6
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:3F************@jpl.nasa.gov...
David White wrote:
For the purposes of the language rules, it's best


It is *not* best. It is not true
and it leads to the kind of confusion expressed by emerth.


I shouldn't have said "language rules". I meant that in terms of OO
programming it makes sense to think of it that way.
to think of each object as having
its own copy of each non-static data and function member.


No. This would imply that you could mix and match methods


Not really. It just means that when you call a method for an object, it
behaves as though it's the object's method.

DW

Jul 19 '05 #7
David White wrote:
Not really. It just means that when you call a method for an object,
it behaves as though it's the object's method.


No.
It behaves as though it's the class's method.
Objects which belong to the same class
do *not* invoke different methods.

Jul 19 '05 #8
David White wrote:
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:3F************@jpl.nasa.gov...
David White wrote:
For the purposes of the language rules, it's best


It is *not* best. It is not true
and it leads to the kind of confusion expressed by emerth.


I shouldn't have said "language rules". I meant that in terms of OO
programming it makes sense to think of it that way.


In what way does it 'make sense' to use such a flawed concept?

In another post on this thread you said: "I think of a class as an
object blueprint."

Your analogy, however useful you may find it, is fundamentally flawed.
Conceptually, a blueprint is the instructions for building something.
The closest thing in C++ is a template - which is in effect a blueprint
for building classes. A class on the other hand is a collection of code
plus a data format. It's that data format which is the blueprint for
building objects, and it contains no code of its own.

As Robert pointed out, sloppy analogies and concepts like the one you're
espousing only lead to confusion. Code and static data members belong
to the class, and non-static data belongs to the instance.

--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"

Jul 19 '05 #9
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...
David White wrote:
I mean that in Smalltalk methods are objects.
C functions are objects as well.
Classes are also objects,


No. In smalltalk (and Java),


My recollection is that everything is an object in Smalltalk.
From the Smalltalk/V manual and encyclopedia of classes, 1992:
"Classes are also objects contained in global variables which are maintained
in the System Dictionary "Smalltalk".
"Every class is an instance of a metaclass of the same name."
In smalltalk (and Java),
an object is instantiated along with the class definition
that you can reference using the same name as the class.
This extra overhead was deemed acceptable to smalltalk designers
because it simplifies the language definition.


DW

Jul 19 '05 #10
Corey Murtagh <em***@slingshot.co.nz.no.uce> wrote in message
news:10***************@radsrv1.tranzpeer.net...
In another post on this thread you said: "I think of a class as an
object blueprint."
That's right.
Your analogy, however useful you may find it, is fundamentally flawed.
Conceptually, a blueprint is the instructions for building something.
Yes, in this case on object of that class. The class defines the object's
data and behaviour.
The closest thing in C++ is a template - which is in effect a blueprint
for building classes. A class on the other hand is a collection of code
plus a data format.
The class defines completely how an instance of it will behave. That's why I
called it a blueprint. It's an appropriate analogy.
It's that data format which is the blueprint for
building objects, and it contains no code of its own.

As Robert pointed out, sloppy analogies
It isn't sloppy at all.
and concepts like the one you're
espousing only lead to confusion. Code and static data members belong
to the class, and non-static data belongs to the instance.


That's what I call confusing. It implies that the code is indepenent of
instances of the class. It's not. You can _only_ call a non-static member
function in the context of an object. Non-static member functions usually
refer to member variables that are owned by an object, not the class.
Therefore, conceptually the function belongs to the object, not the class.

DW

Jul 19 '05 #11

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

Similar topics

66
by: Mike Stenzler | last post by:
I am new to Template programming and I would like to create an array of user-defined class objects using MFC CArray. Normally I would use linked list processing - create an object class and then an...
3
by: Neil Zanella | last post by:
Hello, It seems to me that using too many variables at class scope in C++ (e.g. private data members) can be just as bad as having a C program with lots of global variables. This is especially...
8
by: Jan-Ole Esleben | last post by:
Hi! I am new to this list, and maybe this is a stupid question, but I can't seem to find _any_ kind of answer anywhere. What I want to do is the following: I want to insert a class variable...
4
by: portroe | last post by:
I need to slip a simple method into the following class, the method should display a message box, displaying the entered information.. Public Class employee 'Declaration of variables Private...
1
by: Kenny | last post by:
I am having issues with the "Date.ToOADate" method in VB .NET. I understand that in VB6 the function DateSerial gives an internal representation of the date. When I use the DateSerial method in...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
3
by: Tony Johansson | last post by:
Hello!! You may correct me if I have made any wrong assumptions. Below I have some simple classes. When you have this t.ToString() below it's the ToString() method in Object class that is...
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...
6
by: jmarcrum | last post by:
Hi everyone! I'm using a super class (DVD.java) that handles another class (EnhancedDVD.java). I want to pass the "details" of the DVD into the super class DVD.java. The super class contains 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?
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
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
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.