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: Maett
Subject: Re: how to make a static function a friend of a class
Date: Mon, 16 May 2005 22:19:10 +0200
User-agent: Opera M2/7.54 (Win32, build 3929)

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.
[snip]

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

I hope I do...

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

That's exactly what I want: foo1() should not be visible outside its
source; it should only be called by means of another globally visible
function.


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.

As you assumed my example is minimized to just show the effect.
In my real code the role of the static function foo1() is
played by a virtual constructor which must have access to
private data of the object it constructs.
OTOH the (address of the) virtual constructor is entered in a
struct which consists of some kind of metainfo for the generated
classes (attribute names,unique class number,...). This struct
gets entered in a lookup table at startup (with a "nifty_counter")

I tried the approach to make foo1() a static member of class
foo1Class before. But then I get the problem that I can't access
the private member function out of a struct. To show this error
I had to extend my example a bit:
foo.h:
  struct metainfo {
    int (*getSomeData)();
  };

  class foo1Class {
    public:
      int getPublicData();
    private:
      static int getPrivateData();
      static int foo1();
      static const int n;
      static const int m;
    // friend struct metainfo; // does not help
  };

  void enter(const metainfo* const*);

foo.cpp:
  #include "foo.h"

  const int foo1Class::n = 77;
  const int foo1Class::m = 88;

  int foo1Class::getPublicData()
  {
    return m;
  }

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

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

  static const metainfo foo1Metainfo = {
    foo1Class::getPrivateData // error: getPrivateData is private
  };

  static const metainfo* const info[] = {
    &foo1Metainfo
  };

  void initializer() // will be called exactly once at startup
  {
    enter(info);
  }


I'm afraid I can't declare a struct to be fried of a class, either.


Regards,
Larry



reply via email to

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