?
Solved

C++ class member functions when using an array of classes

Posted on 2013-12-10
4
Medium Priority
?
427 Views
Last Modified: 2013-12-10
Hi,

Let's say I create an array of a dog class. That dog class has member functions, such as eat, sleep etc. Those functions are the same for every instance in the array. To me that seems like a huge waste of memory. How can this be improved?

dog dogs[1000];

dogs[0].eat();
dogs[0].sleep();

It seems like a pointer to the eat and sleep functions would be better, but I'm coming from a C background so I would rather ask the experts rather than reinvent the wheel.

Thanks,
Sean
0
Comment
Question by:sean-keys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Assisted Solution

by:imladris
imladris earned 400 total points
ID: 39709332
Even though there are a thousand objects there aren't actually a thousand copies of the code. The code exists in a central place. What there will be a thousand of is the data and various necessary pointers to keep things separate.
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 400 total points
ID: 39709348
the member functions are defined once for the class. there is no repetition of the code when an array or multiple instances of the class were defined.

when a c++ member function was called it is exactly the same as when a c function was called (as long as it is not a virtual call). the 'this' pointer of the object the member function was associated with, was passed as a (hidden) argument to the function.

Sara
0
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 39709352
>>Those functions are the same for every instance in the array. To me that seems like a huge
>>waste of memory. How can this be improved? [...]
>>It seems like a pointer to the eat and sleep functions would be better, but I'm coming from
>>a C background so I would rather ask the experts rather than reinvent the wheel.

Well, these member functions exist in memory only once, not on a per-object basis. Each of these members is passed a hidden 'this' pointer as their 1st argument, so they know which object they are invoked for.

See also http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/ ("The hidden “this” pointer") which is a quite thourough explanation of the above.
0
 

Author Closing Comment

by:sean-keys
ID: 39709473
Makes sense. Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question