Member vs Non member function

Discussion in 'C++' started by Per, Aug 30, 2004.

  1. Per

    Per Guest

    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
    Per, Aug 30, 2004
    #1
    1. Advertising

  2. "Per" <> wrote in message
    news:...
    > 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.
    Vladimir Ciobanu, Aug 30, 2004
    #2
    1. Advertising

  3. Per

    Mike Wahler Guest

    "Per" <> wrote in message
    news:...
    > 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
    Mike Wahler, Sep 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. slide_o_mix
    Replies:
    0
    Views:
    410
    slide_o_mix
    Oct 15, 2003
  2. Alex
    Replies:
    0
    Views:
    380
  3. John Black
    Replies:
    1
    Views:
    384
    Daniel T.
    May 31, 2004
  4. dolphin
    Replies:
    3
    Views:
    1,326
    Pete Becker
    Dec 5, 2007
  5. ittium
    Replies:
    5
    Views:
    429
    88888 Dihedral
    Jan 12, 2012
Loading...

Share This Page