static

S

stub

In a book the author said:"If you have a member function that accesses only
the static data members of a class, you can declare the function static as
well."

I don't understand that what's the advantages of making a member static
since static member functions can access non-static data in a class no
matter what.

Could someone explain on this? Thanks!
 
M

Moonlit

Hi,


stub said:
In a book the author said:"If you have a member function that accesses only
the static data members of a class, you can declare the function static as
well."

I don't understand that what's the advantages of making a member static
since static member functions can access non-static data in a class no
matter what.
The this pointer doesn't have to be pushed on the stack, saves a very small
amount of time (big deal ;-) )
Could someone explain on this? Thanks!
Regards, Ron AF Greve.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

stub escribió:
I don't understand that what's the advantages of making a member static
since static member functions can access non-static data in a class no
matter what.

You can call it even if you don't have an object of the class.

You can assign it to an ordinary function pointer.

No this pointer passed, then less overhead. This may be important in
some cases.

Regards.
 
T

Tim Slattery

stub said:
In a book the author said:"If you have a member function that accesses only
the static data members of a class, you can declare the function static as
well."
I don't understand that what's the advantages of making a member static
since static member functions can access non-static data in a class no
matter what.

You can call a static member function without instantiating the class.
 
C

Chris Dams

Hello,

stub said:
I don't understand that what's the advantages of making a member static
since static member functions can access non-static data in a class no
matter what.

You can call the static function even if you do not an object to call it.
For instance if you have a class gogame (to store information of a game of
the famous Japanse board game), it may be that you can set the counting
algorithm by calling gogame::useareascoring() or gogame::useterritoryscoring(). That is, if all objects of type gogame are supposed to use the same scoring
method, you can already set this before creating an object of type gogame.
Perhaps creating a gogame object opens a window to view the game, so that
it is undesirable to do this when only setting the scoring algorithm.

Bye,
Chris Dams
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top