static functions

Discussion in 'C++' started by grahamo, Feb 24, 2004.

  1. grahamo

    grahamo Guest

    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
    grahamo, Feb 24, 2004
    #1
    1. Advertising

  2. 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.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Feb 24, 2004
    #2
    1. Advertising

  3. grahamo

    jeffc Guest

    "Julián Albo" <> wrote in message
    news:...

    >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.
    jeffc, Feb 24, 2004
    #3
  4. jeffc escribió:

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


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

    Regards.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Feb 24, 2004
    #4
  5. grahamo

    Rolf Magnus Guest

    Julián Albo wrote:

    > jeffc escribió:
    >
    >> >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.

    >
    > 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 ;-)
    Rolf Magnus, Feb 24, 2004
    #5
  6. grahamo

    jeffc Guest

    "Julián Albo" <> wrote in message
    news:...
    jeffc escribió:

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


    >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.
    jeffc, Feb 24, 2004
    #6
  7. jeffc escribió:

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

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


    "Anything" is a friend?

    Regards.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Feb 24, 2004
    #7
  8. grahamo

    jeffc Guest

    "Julián Albo" <> wrote in message
    news:...
    jeffc escribió:

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

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


    "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.
    jeffc, Feb 24, 2004
    #8
  9. 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.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Feb 24, 2004
    #9
    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. Victor Bazarov

    static and non-static functions

    Victor Bazarov, Apr 2, 2005, in forum: C++
    Replies:
    4
    Views:
    7,150
    Stephen Howe
    Apr 2, 2005
  2. Replies:
    9
    Views:
    934
    Ben Bacarisse
    Feb 6, 2006
  3. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    695
    Army1987
    Oct 8, 2007
  4. mafiQ
    Replies:
    0
    Views:
    465
    mafiQ
    Apr 22, 2009
  5. ssb
    Replies:
    58
    Views:
    1,615
    Chris M. Thomasson
    Sep 14, 2009
Loading...

Share This Page