static functions

G

grahamo

This is sorta how long is a piece of string type of question but here
goes.

I know there are a lot of issues/caveats around use of static
functions. e.g. if your function is static then it cannot be virtual
as this contradicts dynamic binding. Also, a static function cannot
access any attributes within the class it which it defined as the
function exists without any object instances. etc. etc.

Is there any list of such issues, caveats around anywhere? If anybody
has any more like the above, I'd love to hear them.


thanks much

GrahamO
 
?

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

grahamo escribió:
I know there are a lot of issues/caveats around use of static
functions. e.g. if your function is static then it cannot be virtual
as this contradicts dynamic binding. Also, a static function cannot
access any attributes within the class it which it defined as the
function exists without any object instances. etc. etc.

A static function can access any attributes of the class, if it has
access to an object, or pointer or reference to an object, of the class.
It has no direct access because do not have a this pointer, but indirect
access is allowed.

Regards.
 
J

jeffc

A static function can access any attributes of the class, if it has
access to an object, or pointer or reference to an object, of the class.
It has no direct access because do not have a this pointer, but indirect
access is allowed.

That is true of anything, not just static functions, so there's not much
sense in pointing it out relative to the original question.
 
?

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

jeffc escribió:
That is true of anything, not just static functions, so there's not much
sense in pointing it out relative to the original question.

Not at all, a static function can access private data of the class, any
external function can't.

Regards.
 
R

Rolf Magnus

Julián Albo said:
jeffc escribió:


Not at all, a static function can access private data of the class,
any external function can't.

This is not true either. Friends can still access it ;-)
 
J

jeffc

jeffc escribió:
That is true of anything, not just static functions, so there's not much
sense in pointing it out relative to the original question.
Not at all, a static function can access private data of the class, any
external function can't.

It certainly can, if it is a friend.
 
J

jeffc

jeffc escribió:
It certainly can, if it is a friend.

"Anything" is a friend?


You said a static function can access any attributes of the class, if it has
access. Anything can access any attributes of the class, if it has access.
A friend function has access. Anything that has access has access. I just
thought your answer was going to be confusing to the OP. He seemed to
understand the basic idea that a static function is not automatically
associated with an instance of a class like a member function is.
 
?

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

jeffc escribió:
I just thought your answer was going to be confusing to the OP. He seemed
to understand the basic idea that a static function is not automatically
associated with an instance of a class like a member function is.

I think the contrary. I have seen people seeing a static function using
a private member of an object of his class and saying they have readed
that the static have no access to the non static members.

Regards.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top