Workaround for accessing non-static members inside static member functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Workaround for accessing non-static members inside static member functions

  1. #1
    Join Date
    May 2010
    Posts
    12

    Workaround for accessing non-static members inside static member functions

    What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B.

    Any pointers?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,383

    Re: Workaround for accessing non-static members inside static member functions

    Can you post a simple example of what you are trying to achieve.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Workaround for accessing non-static members inside static member functions

    Quote Originally Posted by hemant.bhargava7 View Post
    What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B.

    Any pointers?
    To access the non-static member variables of some class wrom anywhere you need to have a pointer or reference to some instance of the class which member you are going to access.
    Victor Nijegorodov

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Workaround for accessing non-static members inside static member functions

    Quote Originally Posted by hemant.bhargava7 View Post
    What are the workarounds for accessing the non-static member variables of some class(Say A)
    There is no such thing as a "workaround" here. The rules of C++ are specific -- to access non-static members of a class from outside the class itself:

    a) The members must have an access specifier that makes them accessible (public for unrelated classes, public / protected for derived classes).
    b) You must have an instance of the class A (as VictorN pointed out) to access non-static members.

    Also on a design level, why are you allowing the client (class B) to access member variables of class A? Accessing member variables directly breaks encapsulation, i.e. if that member variable changes (for example, the name changes), the client code has to change. Better to use get() / set() functions and have the client call those functions instead of accessing member variables directly.
    Class A is derived with public properties of class B.
    The only C++ terms in that sentence are "class" "derived" and "public". There is no such thing as "properties" in C++. So I don't understand what you're trying to say here.

    Are you saying that B is publicly derived from A?
    Code:
    class A
    {
    };
    
    class B : public A
    {
    };
    Is it this?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2010
    Posts
    12

    Re: Workaround for accessing non-static members inside static member functions

    Quote Originally Posted by Paul McKenzie View Post
    There is no such thing as a "workaround" here. The rules of C++ are specific -- to access non-static members of a class from outside the class itself:

    a) The members must have an access specifier that makes them accessible (public for unrelated classes, public / protected for derived classes).
    b) You must have an instance of the class A (as VictorN pointed out) to access non-static members.

    Also on a design level, why are you allowing the client (class B) to access member variables of class A? Accessing member variables directly breaks encapsulation, i.e. if that member variable changes (for example, the name changes), the client code has to change. Better to use get() / set() functions and have the client call those functions instead of accessing member variables directly.
    The only C++ terms in that sentence are "class" "derived" and "public". There is no such thing as "properties" in C++. So I don't understand what you're trying to say here.

    Are you saying that B is publicly derived from A?
    Code:
    class A
    {
    };
    
    class B : public A
    {
    };
    Is it this?

    Regards,

    Paul McKenzie
    Paul, Again thanks for quick reply. Yes. It is same as you expected.
    class A {
    public:
    var_a;
    }
    class B : public A {
    static func_a();
    }
    B::func_a() { // Need to use a; }

  6. #6
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Workaround for accessing non-static members inside static member functions

    Quote Originally Posted by hemant.bhargava7 View Post
    B::func_a() { // Need to use a; }
    This is rule b) I mentioned above. There is no instance of an A or B object in that code.

    Regards,

    Paul McKenzie

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: Workaround for accessing non-static members inside static member functions

    Conceptually it doesn't make any sense. Since it isn't static, a distinct var_a exists for every instance of A. If there are multiple instances, there are multiple var_a variables. If no instances exist, neither does var_a. What value are you expecting it to hold, or how are you expecting it to hold a value at all?

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,383

    Re: Workaround for accessing non-static members inside static member functions

    In order for the static function func_a defined as part of class B to access the variable var_a defined in class A, the static function func_a needs either a reference or a pointer to the required instance of class A in order to obtain the required value. As var_a defined in class A is not static, its value is only defined as part of a class instance (ie when a class variable is defined).

    The code below is an example of using int_a from func_a

    Code:
    class A {
    public:
    	int var_a;
    
    	A() : var_a(0){}
    };
    
    class B : public A {
    public:
    	static int func_a(A* ia);
    };
    
    int B::func_a(A *ia)
    {
    	// Need to use a;
    	return (ia->var_a);
    };
    
    int main()
    {
    A	ia;
    
    	B::func_a(&ia);
    }
    Last edited by 2kaud; September 11th, 2013 at 03:30 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center