Member vs Non member function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Per

    #1

    Member vs Non member function

    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
  • Vladimir Ciobanu

    #2
    Re: Member vs Non member function


    "Per" <per.larsson@gm ail.com> wrote in message
    news:f67edfbc.0 408292334.34eef ebe@posting.goo gle.com...[color=blue]
    > 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[/color]

    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.


    Comment

    • Mike Wahler

      #3
      Re: Member vs Non member function


      "Per" <per.larsson@gm ail.com> wrote in message
      news:f67edfbc.0 408292334.34eef ebe@posting.goo gle.com...[color=blue]
      > Hi!
      > Does it take longer time to initialize a class[/color]

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

      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.
      [color=blue]
      > Will the sizeof (Some Class) return more bytes on a class with more
      > member-functions etc...[/color]

      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


      Comment

      Working...