help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to make a static function a friend of a class


From: Larry I Smith
Subject: Re: how to make a static function a friend of a class
Date: Mon, 16 May 2005 17:10:03 GMT
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.7) Gecko/20050414

Maett wrote:
Larry I Smith wrote:

Maett wrote:

Hi.

I would like to access private class data out of a static function as
follows:

foo1.h:
  class foo1Class {
    public:
      static int getN();
    private:


         friend int ::foo1();

      // friend int foo1();
      static const int n;
  };

foo1.cpp:
  #include "foo1.h"

  const int foo1Class::n = 77;

  int foo1Class::getN()
  {
    return foo1();
  }


     int foo1()


But I would like foo1 to be static to avoid it being a global symbol.
(I have a tool which generates c++ code out of data structure definitions, and there are quite a few such definitions, which would lead to manymany global symbols. Unfortunately I am stuck to g++ 3.2.3 for the moment, so I can not use the
  __attribute__ ((visibility("hidden")))
mechanism of g++ 3.4 and later.)

[snip]

I'm not sure you understand the differences between a 'static'
member of a class and a 'static' function that is NOT a member
of a class.

In your implementation file (foo1.cpp), if you make foo1()
'static' then foo1() is only visible to other functions within
foo1.cpp - it will not be visible in any other source files
that comprise your application; only the functions inside foo1.cpp
will be able to call foo1().

Another approach is to make foo1() a static member of class
foo1Class:

foo1.h:
  class foo1Class {
    public:
      static int getN();
    private:
      static int foo1();
      static const int n;
  };

foo1.cpp:
  #include "foo1.h"

  const int foo1Class::n = 77;

  int foo1Class::getN()
  {
    return foo1();
  }

  int foo1Class::foo1()
  {
    return foo1Class::n;
  }


In this scenerio there will be only one foo1(),
and only one getN() for foo1Class - regardless
of how many foo1Class objects are created.

I'm assuming that your examples are 'made up' and
bear little resemblence to you actual code, because
foo1() is not actually needed at all - since getN()
could return foo1Class:n directly without the need
to call foo1().

It appears that your tool is generating extremely
poor C++ code.

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]