CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2008
    Posts
    26

    Question isolate a static method

    Hello everybody!

    Here is my problme description.
    In my class I have a method which is static, but which, in the same time has to access some data members of my class. Conseqeuntly, all the data members it uses become static, plus other methods of the class...so...everything is transforming, little by little, to static... which is something I obviously do not want to...

    I am wondering if there is some design pattern which could help me isolate the static method.

    I have to mention that the reason why the method is static is because I need a reference/pointer to it, which has to be known at the run time.

    Thank you in advance,
    D.M.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: isolate a static method

    Quote Originally Posted by munteanu24d View Post
    Hello everybody!

    Here is my problme description.
    In my class I have a method which is static, but which, in the same time has to access some data members of my class. Conseqeuntly, all the data members it uses become static, plus other methods of the class...so...everything is transforming, little by little, to static... which is something I obviously do not want to...

    I am wondering if there is some design pattern which could help me isolate the static method.

    I have to mention that the reason why the method is static is because I need a reference/pointer to it, which has to be known at the run time.

    Thank you in advance,
    D.M.
    You explanation is confusing me, so I'll try to explain the best I can.

    You can have pointers to member functions, so I don't see the problem.

    If you are affraid of pointers to member and insist on static, why not just make an instance of your class an argument to your function?

    Code:
    class my_class
    {
    public:
      static void static_multiply_by_two(my_class& object)
      {
        int val = object.get();
        val *= 2;
        object.set(val);
      }
    
      void set(int);
      int get() const;
    private:
      int _i;
    }
    An even better alternative is to make the implementation in a member function, and have the static be nothing more than a wrapper. The advantage is that this also works if you have a class hierarchy, and want a virtual member function pointer.

    Code:
    class my_class
    {
    public:
      static void call_member_function_as_static(my_class& object)
      {
        object.member_function();
      }
    
      virtual void member_function();
    
    }
    I hope this helps.

    Could you post your class, so we might have a better idea of what yo are trying to achieve?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Dec 2008
    Posts
    26

    Re: isolate a static method

    This is the function signature which I cannot change, because the following StartServer method
    is waiting for a reference to a function with exactly that signature. So I cannot add any additional paramater to AnswerTo Connection.

    Code:
    static int AnswerToConnection (void *cls, struct Connection *connection, 
                                            const char *url, const char *method, const char *version,
                                            const char *upload_data, size_t *upload_data_size, void **ptr);
    This is where I need the reference to my function;
    Code:
    mServer = mDaemonServer->StartServer(USE_SELECT_INTERNALLY, PORT, NULL, NULL, 
             &AnswerToConnection, (void *)ASK_PAGE,OPTION_END);
    I also tryed to remove the static keyword from AnswerToConnection and get a reference to the method in this why, but i still does not work.
    Code:
    static AccessHandler pRqUpdateFn = &HTTPServer::AnswerToConnection; 
    mServer = mDaemonServer->StartServer(USE_SELECT_INTERNALLY, PORT, NULL, NULL, 
             pRqUpdateFn, (void *)ASK_PAGE,OPTION_END);

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: isolate a static method

    Quote Originally Posted by munteanu24d View Post
    This is the function signature which I cannot change, because the following StartServer method
    is waiting for a reference to a function with exactly that signature. So I cannot add any additional paramater to AnswerTo Connection.
    Normally, a framework that uses function pointers like this allows for an extra parameter (often as void*) that can be used to pass the class instance in a generic way. When passing the function pointer, you cast the this pointer to a void* and in the callback function, you do the reverse cast.
    Code:
    class MyClass
    {
        static void Callback(void* extra_param) {
            MyClass* pThis = reinterpret_cast<MyClass*>(extra_param);
            pThis->MemberFunction();
        }
    
        void MemberFunction(); // does the real work
    };
    
    int main()
    {
        MyClass c;
        // call function that takes function pointer
        // pass pointer to class instance as extra parameter
        Register(&MyClass::Callback, reinterpret_cast<void*>(&c));
    }
    Check the documentation of the framework you are using to see which parameter you can use to pass custom data.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Dec 2008
    Posts
    26

    Re: isolate a static method

    Thank you very much for your answer. Yes, my function really has a void* extra argument, but I have to admit that I did not how to use it...

  6. #6
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: isolate a static method

    Quote Originally Posted by munteanu24d View Post
    Thank you very much for your answer. Yes, my function really has a void* extra argument, but I have to admit that I did not how to use it...
    You pass 'this' instead of one of the NULL arguments of StartServer (look at the prototype to find out which argument it is) and the StartServer will pass that pointer to the server which will return it (most probably) as first argument when calling AnswerToConnection.

    If this is right so far you could have code like

    Code:
    MyClass * p = (MyClass*)cls;
    at the beginning of the AnswerToConnection function and thus you have a pointer to the class where you called the StartServer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured