Access other members within same nested class?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Access other members within same nested class?

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    105

    Question Access other members within same nested class?

    It's hard to give a precise title but here is the question in detail: I have a class, something like this:
    Code:
    class classA{
      public: 
        void fnA();
        ...
    };
    and another class that contains objects of classA:
    Code:
    class classB{
      public:
        classA A1;
        classA A2;
        classA A3;
        vector<classA*> vA;
        ...
    };
    classB B1;
    Now is it possible to access B1.vA from B1.A1.fnA() through some kind of pointer chain like this->parent->vA ? If so, how? Thanks!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Access other members within same nested class?

    Unless you want us to fill in the "..." with code, classA has no idea it is being stored in a vector, list, map, etc. So the answer to your question is no.

    Also, to make it easy for others, here is a real, compilable version of the code, without the "..." and with the proper includes.
    Code:
    #include <vector>
    
    class classA
    {
        public:
           void fnA() {}
    };
    
    class classB
    {
      public:
        classA A1;
        std::vector<classA*> vA;
    };
    
    int main()
    {
       classB B1;
       B1.A1.fnA();
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,999

    Re: Access other members within same nested class?

    Quote Originally Posted by acppdummy View Post
    It's hard to give a precise title but here is the question in detail: I have a class, something like this:
    Maybe you should try describing your actual problem, rather than such a theoretical case. Whenever a design is such that an object needs to know about the context in which it is stored, I hear bells ringing. Maybe you should rethink your design.
    Quote Originally Posted by acppdummy View Post
    Now is it possible to access B1.vA from B1.A1.fnA() through some kind of pointer chain like this->parent->vA ? If so, how? Thanks!
    classA needs to have a pointer to an instance of classB in order to do this. That's it.
    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

  4. #4
    Join Date
    Nov 2010
    Posts
    105

    Re: Access other members within same nested class?

    Thank you for the quick reply! Yes I will have to redo the design.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,643

    Re: Access other members within same nested class?

    the C++ answer: No.
    there is only a "parent"/"child" relationship with the direct class inheritance.

    the C++ language does not have any built in provisions at all for establishing a relationship of members to the class they are a member of. In the above example, there is no relationship between ClassB and ClassA or ClassB and the vA vector.

    That is to say, the compiler doesn't provide such a relationship mechanism, you can of course provide it for yourself, but that means extra members and you'll need to initialise those members properly.


    The non C++ answer:
    if you don't care about portablity to another compiler, or even another version of the same brand of compiler and even changes in compiler settings (changing any of these will need code revision).
    Then you can do this by using the direct memory layout. If you know by some means in your code that the vA must be a part of a ClassB, then you can do some pointer math to calculate the ClassB instance pointer from the vA instance pointer.
    P.S. Whoever will need to maintain this code will hate you for this.

  6. #6
    Join Date
    Nov 2010
    Posts
    105

    Re: Access other members within same nested class?

    Thank you! I have changed the vector to a static member of classA instead.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,643

    Re: Access other members within same nested class?

    making a vector static would allow access, but it changes the nature of things.
    by making it static, there is only 1 vector regardless of how many objects you make. without static, there is a vector in each individual instance.

    static may be just what you wanted/needed, but it is not the same as the question in the OP.

  8. #8
    Join Date
    Nov 2010
    Posts
    105

    Re: Access other members within same nested class?

    Yes in my particular situation I need a global list of certain objects of classA so a static member just fits the need. Thanks!

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center