Solved

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

Posted on 2013-12-10
4
424 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 100 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 34

Assisted Solution

by:sarabande
sarabande earned 100 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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 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