Can a inner class have a member of outer class type?

This is a discussion on Can a inner class have a member of outer class type? within the C++ Programming forums, part of the General Programming Boards category; Hi, for example Code: class A { class B { A mA; // VS complains that A is undefined } ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    Can a inner class have a member of outer class type?

    Hi,

    for example

    Code:
    class A
    {
        class B
        {
              A mA;    // VS complains that A is undefined
        }
    };
    Is there any way around?

    Thank you in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Why does B need an A object as a member?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    A defines a structure and B defines some presentation, read- and write functions to it. Therefore B uses some state (member) variables which shall not be member of A, so it needs to be an own class.

    I could of course just do

    Code:
    class A
    {
    
    };
    
    class B
    {
          A mA;    // VS complains that A is undefined
    }
    ...which would rather be the same, at least for my. Why not for the compiler?

    I would like to use the inner class because this way it's in the namespace defined by the outer class.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This still doesn't explain why B needs an instance of A, rather than storing (or being passed) a reference/pointer to an A.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    The data is stored on a server, B need to receive it. You can't hold remote data per reference.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by pheres
    A defines a structure and B defines some presentation, read- and write functions to it. Therefore B uses some state (member) variables which shall not be member of A, so it needs to be an own class.
    It sounds as if B is redundant and you should move the member functions of B to A.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by pheres View Post
    B uses some state (member) variables which shall not be member of A, so it needs to be an own class.
    That means B's members functions depend on each other, so B needs it's own member variables. But the can't be part of A because A is needed by legacy code as well and this code relies on the memory layout of A.

    I guess there is no solution to the original problem and I've to split A and B.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by pheres
    I guess there is no solution to the original problem and I've to split A and B.
    Yeah, perhaps as a workaround you can put them both in the same namespace.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Short answer: No. If A has B as a member, B cannot have A as a member. Why? It would be infinite recursion. B can have a pointer to type A, but it cannot have an instance of A.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    B is no member of A. B is an inner class to A.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 11:55 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21