Solved

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

Posted on 2013-12-10
4
420 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
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 100 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
Makes sense. Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now