Member vs Non member function

P

Per

Hi!
Does it take longer time to initialize a class with many
member-functions
than one with fewer?
Will the sizeof (Some Class) return more bytes on a class with more
member-functions etc...
/p
 
V

Vladimir Ciobanu

Per said:
Hi!
Does it take longer time to initialize a class with many
member-functions
than one with fewer?
Will the sizeof (Some Class) return more bytes on a class with more
member-functions etc...
/p

No, it will not. This is all taken care of by the compiler - obviously
at compiletime. The size of your class should not increase.
Deciding whether a function should be a member is usually a design
decision and has nothing to do with optimisations.

Usually, unless there's a very good reason to make it a member
function [Example:
- you need to access private members that you can't otherwise
- you need to override a virtual function
- it can't be implemented using the current class' public interface
- it is an operator that requiers to be a member], you should probably
not make it a member. It is better to have as simple as possible (but
not simpler) interfaces to keep things clear.

Vladimir Ciobanu.
 
M

Mike Wahler

Per said:
Hi!
Does it take longer time to initialize a class

A class cannot be initialized. A class specifies a type.
An object of class (or any other) type can be intitialized.
with many
member-functions
than one with fewer?

The language does not specify at all how long
a particular construct takes to execute. This depends
entirely upon the implementation and the host platform.
But in practice, I would not expect any performance difference
between initializing an object of a type with no member functions
and one with many.
Will the sizeof (Some Class) return more bytes on a class with more
member-functions etc...

No, the count of member functions does not affect the value
returned by applying 'sizeof' to the type containing those
member functions. It will report a size at least equal to
(but often greater than) the sum of the sizes of its data
members. The minimum it will report is a size of 1, even
for an 'empty' class such as:

class X
{
};

sizeof(X); /* yields a value >=1 */


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top