classes within classes?

This is a discussion on classes within classes? within the C++ Programming forums, part of the General Programming Boards category; yet another question.... this program is driving me nuts. anyway... i have a class (CMessages) that i want to have ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    classes within classes?

    yet another question.... this program is driving me nuts. anyway...

    i have a class (CMessages) that i want to have encapsulated within another class (CUser). i also want to have the encapsulated class have access to the members of the outer class, but when i try to use them i get errors saying "illegal implicit conversion from CUser::CMessages to CUser." this is something i've done plenty of in java and c# but i have no idea how to do it in c++, or if it is even possible.... can anyone help?


    ... praying that 2 answered questions in 1 day isn't too much to ask.....

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    It does not work that way in C++. In Java, I believe the inner class has access to the members of the specific instance of the outer class. In C++, nesting a class only defines the scope in which it lives. There is no specific instance of the inner class tied to the outer class, unless you specifically make one, and even then the inner class instance has no way of referring to the outer class instance's specific members.

    If you make the inner class a friend, it will have access to the private data of the outer class, but you still must somehow provide it a reference to an existing instance to work on.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    ah right... i completely forgot about the whole friend concept. again, thanks so much daved. i really appreciate the help.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    An inner class in C++ that you declare as a friend works the same as a static inner class in Java.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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