×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Regular Contributor
    Join Date
    Jun 2008
    Posts
    346

    How to call non static variable inside a static function?

    Others say i should pass the value but I can't since its a callback I cant change the parameter passed by the function because if i do so the callback function will not satisfy the condition of the callback

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: How to call non static variable inside a static function?

    In Symbian C++ you can already find callbacks:
    - one kind of callback is a static method accepting a TAny* argument, then it cast the pointer to the proper class, and invokes a non-static method on it. Such callbacks are wrapped into a TCallBack object (so you do not have to invent it), CPeriodic is a rather common example for that
    - the others are the methods inherited from M-classes, in that case you pass the "this" pointer as a known M-type, and when it is going to be invoked, it will automatically run in the context of your full object

    Anyway, what kind of callback do you have?

  3. #3
    Regular Contributor
    Join Date
    Jun 2008
    Posts
    346

    Re: How to call non static variable inside a static function?

    But the callback was a callback from a library which is ported to symbian. its PJSIP. The call back has its own form and parameter

  4. #4
    Registered User
    Join Date
    Apr 2005
    Location
    Barcelona
    Posts
    1,678

    Re: How to call non static variable inside a static function?

    Static functions are not associated with instances, so you need some way to pass "this" (ptr to the object) to them.

  5. #5
    Super Contributor
    Join Date
    May 2006
    Location
    Beijing,China
    Posts
    1,190

    Re: How to call non static variable inside a static function?

    Quote Originally Posted by joseph.m View Post
    Others say i should pass the value but I can't since its a callback I cant change the parameter passed by the function because if i do so the callback function will not satisfy the condition of the callback
    no way. or I do not quite understand.

    static function does not related to this pointer.
    and the non-static varible in stactic function are auto varibles. They has no value, or even no definitions without calling the statci function.
    [I][COLOR="Orange"]xhsoldier[/COLOR][/I]
    [email]xhsoldier@gmail.com[/email]/[email]xhsoldier@163.com[/email]

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: How to call non static variable inside a static function?

    Several terrible workarounds may still exist, depending on how many instances exist of your event handler class. If only one, you can create an explicit (storing its pointer into a static variable, TLS, or inheriting from CCoeStatic) or implicit (returning it from AppUi for example, which is always accessible via CCoeEnv::Static()->AppUi()) singleton of it.

  7. #7
    Regular Contributor
    Join Date
    Jun 2008
    Posts
    346

    Re: How to call non static variable inside a static function?

    OK here is the code:

    in my MyClassView.h
    Code:
    class CMyClass
    
    class CMyClassView {
    private:
    ...
    ...
    ...
    ptotected:
    ...
    ...
    ...
    public:
    CMyClass* iMyClass;
    static void on_buddy_state(pjsua_buddy_id buddy_id);
    ...
    ...
    ...
    };
    In my MyClassView.cpp
    Code:
    ...
    ...
    ...
    
    void CMyClassView::on_buddy_state(pjsua_buddy_id buddy_id)
    {
    //I WANT TO CALL HERE THE iMyClass because I want to call one of its method..
    }
    
    
    void CMyClassView::app_start()
    {
    pjsua_config cfg;
    cfg.cb.on_buddy_state=&on_buddy_state; //this is the line that sets the call back function
    }

  8. #8
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: How to call non static variable inside a static function?

    Use a global variable as a first attempt. I would think that you will not have multiple views, so simplu try "static CMyClass* iMyClass;".

  9. #9
    Super Contributor
    Join Date
    May 2006
    Location
    Beijing,China
    Posts
    1,190

    Re: How to call non static variable inside a static function?

    Quote Originally Posted by wizard_hu_ View Post
    Use a global variable as a first attempt. I would think that you will not have multiple views, so simplu try "static CMyClass* iMyClass;".
    Yes, a simple idea is that:
    the member varibles in a class called by a static function in a class should always be static for that varible.

    or try to simulate the "This" pointer
    [I][COLOR="Orange"]xhsoldier[/COLOR][/I]
    [email]xhsoldier@gmail.com[/email]/[email]xhsoldier@163.com[/email]

  10. #10
    Regular Contributor
    Join Date
    Jun 2008
    Posts
    346

    Re: How to call non static variable inside a static function?

    I tried declaring it static but it returns an error, should i declare it outside of the class? I also tried using this but it returns an error as well.

  11. #11
    Super Contributor
    Join Date
    May 2006
    Location
    Beijing,China
    Posts
    1,190

    Re: How to call non static variable inside a static function?

    Quote Originally Posted by joseph.m View Post
    I tried declaring it static but it returns an error, should i declare it outside of the class? I also tried using this but it returns an error as well.
    I have tested it. No problem.
    static member function should call static member varible in a class.
    [I][COLOR="Orange"]xhsoldier[/COLOR][/I]
    [email]xhsoldier@gmail.com[/email]/[email]xhsoldier@163.com[/email]

Similar Threads

  1. Call back function not working
    By sram_mohan in forum Symbian
    Replies: 1
    Last Post: 2008-05-27, 13:51
  2. A problem in call a function
    By aamitgupta in forum Symbian
    Replies: 3
    Last Post: 2007-12-06, 10:15
  3. Replies: 1
    Last Post: 2004-05-28, 17:09
  4. How to call a sub.jar from inside a main.jar in J2ME
    By s1459193 in forum Mobile Java General
    Replies: 1
    Last Post: 2003-08-15, 13:09
  5. call an application from inside another
    By smallfish_ntu in forum Symbian
    Replies: 1
    Last Post: 2003-05-19, 05:54

Posting Permissions

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