This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C2000 v6.2.11 c++ compiler: Is a member function/variable address affected by it scope, i.e. public or private

Guru 20045 points

Hello,

I am wanting to unit test private member functions and variables in an almost complete program (the only change would be to main where I call the unit test functions).  To do so I have changed the private class members to public class member.  

A test on simple program indicates that the address doesn't change.

Is that always true?

Thanks,
Stephen

  • I would not expect them to change, but you're not supposed to use the raw addresses of C++ member objects. You're supposed to use "pointer to member" types, but these are rarely used. Why do you need to know?
  • "I would not expect them to change"

    That's good to hear.

    "but you're not supposed to use the raw addresses of C++ member objects. You're supposed to use "pointer to member" types, but these are rarely used."

    Why do you say I need to us "pointer to member types".  Given that I created an object var from a class named VAR that has member variables int a,b,c;, I can access the member variables using VAR.a, VAR,b and VAR.c.  I can also access the member functions without using pointers.

    "Why do you need to know?"

    I going to run unit tests with all the release code intact.  For this situation, I want the heap variables and functions to stay in the same locations as the released code, and I will put the unit test go in a separate location in memory. 

     

  • Yes, one most commonly uses . or -> references from a known object to access the members.
    You could also create a pointer to such a member: "int *ptr = &VAR.a;" and use that.
    However, in the level of abstraction C++ expects you to write for, you shouldn't need to know the position of the member relative to the beginning of the class, and thus relative to each other.
    I had guessed about what you were trying to do with the address information, and I assumed it was to implement what "pointer to member" does, and I was wrong about that guess.
    I don't know for sure what the C++ standard says about the order and layout of the data members of a class, other than that all of the parent classes's members must come before the child classes's members. I also doubt the TI compiler would use private vs. protected vs. public to change the layout at all. However, confirming this would require research.