By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,188 Members | 1,775 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,188 IT Pros & Developers. It's quick & easy.

member functions

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Wenjie" <go****@yahoo.com> wrote in message
news:d2**************************@posting.google.c om...
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.
Jul 19 '05 #2

P: n/a
"Wenjie" <go****@yahoo.com> wrote in message
news:d2**************************@posting.google.c om...
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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.