[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-12-10
4
Medium Priority
?
432 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
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

829 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