member functions

Discussion in 'C++' started by Wenjie, Jul 1, 2003.

  1. Wenjie

    Wenjie Guest

    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
    Wenjie, Jul 1, 2003
    #1
    1. Advertising

  2. "Wenjie" <> wrote in message
    news:...
    > 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 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.
    Josephine Schafer, Jul 1, 2003
    #2
    1. Advertising

  3. "Wenjie" <> wrote in message
    news:...
    > 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.


    <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>

    > 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?


    The most obvious difference is that static member functions can be called
    without an object like so <class>::<function>(), whereas non-static member
    functions must be called via an object, like so: <object>.<function>()
    Also remember that static member functions can not access non-static class
    members, whereas non-static member functions can access them.

    > I have a problem behind the question: for some reason the object
    > has to be created with attributes under most of the cases,


    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)

    > 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).


    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.

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


    I don't have any problem understanding your English.

    > Best regards,
    > Wenjie
    Klaus Eichner, Jul 1, 2003
    #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. Hamish
    Replies:
    3
    Views:
    555
    Alf P. Steinbach
    Jan 25, 2008
  2. flounder
    Replies:
    4
    Views:
    499
    flounder
    Apr 2, 2009
  3. Hicham Mouline
    Replies:
    0
    Views:
    423
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    406
    Michael DOUBEZ
    Apr 24, 2009
  5. paul
    Replies:
    8
    Views:
    700
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page