Solved

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

Posted on 2013-12-10
4
421 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 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now