member functions

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

    member functions

    Hello,


    I read someone posted assertions that even the (public) member
    function is not static, there are probably only one copy of the
    code in the executable. Then except the dependency/independency
    on the lifecycle of the object, what is the significant
    differences between public member functions and public static
    member functions?

    I have a problem behind the question: for some reason the object
    has to be created with attributes under most of the cases, but
    sometimes some of the member function is expected from outside
    but at that case there are no object available since the attributes
    input are not ready(but has no impact on the expected function).

    Am I lost in the English instead of C++?


    Best regards,
    Wenjie
  • Josephine Schafer

    #2
    Re: member functions


    "Wenjie" <gokkog@yahoo.c om> wrote in message
    news:d2804eb3.0 307010310.6fdbd 2e5@posting.goo gle.com...[color=blue]
    > Hello,
    >
    >
    > I read someone posted assertions that even the (public) member
    > function is not static, there are probably only one copy of the
    > code in the executable. Then except the dependency/independency
    > on the lifecycle of the object, what is the significant
    > differences between public member functions and public static
    > member functions?[/color]

    I think you want to ask the semantic difference between a normal member
    function and a static member function.

    Static member functions apply to a class while member functions make sense
    for individual class objects.
    For e.g.. a Car class may have a public static member function int
    totalcount ()
    which returns the total number of Car objects created. Now this function
    makes sense only for
    Car class and not for individual Car objects. As opposed to this some
    function like void drive () makes
    sense only for objects of Car class and not for the class itself so it
    should be a normal member function.

    HTH.


    Comment

    • Klaus Eichner

      #3
      Re: member functions

      "Wenjie" <gokkog@yahoo.c om> wrote in message
      news:d2804eb3.0 307010310.6fdbd 2e5@posting.goo gle.com...[color=blue]
      > Hello,
      >
      >
      > I read someone posted assertions that even the (public) member
      > function is not static, there are probably only one copy of the
      > code in the executable.[/color]

      <long text about number of copies in an executable...>
      As far as I am aware, compilers generate in general only one copy of a
      member function in the executable, for both static and non-static member
      functions, public or private alike. You should, however, consider inlined
      and non-inlined functions: inlined member functions are generated in many
      places in the executable, i.e. once for every call of that member function,
      whereas non-inlined member functions are only generated once. You can
      recommend to the compiler to inline a member function by placing its
      definition inside the body of a class, but the compiler is free to decide
      whether the function code is actually inlined or not.
      The number of copies of a function code in the executable makes no
      difference what so ever to the beheaviour or functionality of the program.
      It does, however, impact the performance, i.e. size of the executable and
      run-time.
      <end of long text>
      [color=blue]
      > Then except the dependency/independency
      > on the lifecycle of the object, what is the significant
      > differences between public member functions and public static
      > member functions?[/color]

      The most obvious difference is that static member functions can be called
      without an object like so <class>::<funct ion>(), whereas non-static member
      functions must be called via an object, like so: <object>.<funct ion>()
      Also remember that static member functions can not access non-static class
      members, whereas non-static member functions can access them.
      [color=blue]
      > I have a problem behind the question: for some reason the object
      > has to be created with attributes under most of the cases,[/color]

      You probably mean that in most cases the program creates objects of a
      certain class (this class, of course, has attributes such as member
      functions and data members)
      [color=blue]
      > but
      > sometimes some of the member function is expected from outside
      > but at that case there are no object available since the attributes
      > input are not ready(but has no impact on the expected function).[/color]

      You probably mean that the program calls a member function of a class
      without an object

      Here is the conclusion: in order to call a member function without an
      object, this member function has to be static.
      [color=blue]
      > Am I lost in the English instead of C++?[/color]

      I don't have any problem understanding your English.
      [color=blue]
      > Best regards,
      > Wenjie[/color]


      Comment

      Working...