member functions

W

Wenjie

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
 
J

Josephine Schafer

Wenjie said:
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.
 
K

Klaus Eichner

Wenjie said:
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.
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top