Thread: Nested classes

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Nested classes

    I don't use these very often so let me see if I get this right:

    Code:
    class Surround
    {
      int value;
      public:
        class Within
        {
          public:
            int x;
            Within();
        };
        
         Surround();
    };
    • Surround::Within is public
    • Within cannot access Surround::value since value is private to Surround and Within is not a friend of Surround
    • Surround can access Within::x since Within::x is public
    • Within can be accessed from outside of Surround since Within is public and Within's constructor is public, therefore Surround::Within can be instantiated by anyone
    • Surround can be instantiated by anyone


    The public, protected, and private keywords within Surround refer to how the nested classes will be inherited by derived classes and is not related to nested class access rights in relation to the surrounding class.

    So in this derived class:

    class MyClass:public Surround { };

    MyClass cannot access Surround::value since it is private to Surround.
    MyClass inherits Surround::Within as a public class.
    MyClass can access Surround::Within::x since x is public in Surround::Within.


    Nested classes are quite confusing.
    Last edited by VirtualAce; 09-05-2007 at 02:01 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> MyClass inherits Surround::Within as a public class.
    Is MyClass::Within valid?

    Think of nested classes as being the same as variables when it comes to access specifiers. You can access a public variable or nested class anywhere. You can access a protected variable or nested class only from the current class or derived classes. You can access a private variable or nested class only from the current class.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Nested classes are exactly the same as normal classes in all regards - except that access to the definition is governed by the outer class's access specifiers. In particular, there are no implicit friend relations of any kind. This may change in C++09, though - I believe there will be an implicit bidirectional friendship between a nested and its outer class.
    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

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    This may change in C++09
    I've seen references to different versions of C & C++ (C89, C99, C++09...), and was wondering if anyone has a list of all the different versions and the differences between them? (not the whole specs! just a simple list of the differences)

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    ISO has the full specs, of course. The newer specs typically have lists of changes from the previous versions.

    C89 (or C90, the two are synonymous: 1989 was the year the ANSI standard was published, 1990 it was the ISO standard) is the version of C as described in the ISO standard from 1990. C99 as described in the ISO standard from 1999.

    C++98 is the version of C++ as described in the ISO standard from 1998. C++03 is described in the slightly revised standard from 2003 (incorporating only clarifications and resolutions of errata), but the two are rarely distinguished. C++09 will be described in the upcoming standard, which they hope to release in 2009 (thus C++09 - until half a year ago, it was called C++0x, and if we're unlucky, it will become C++10).

    The differences between C90 and C99:
    http://www.open-std.org/jtc1/sc22/wg14/www/newinc9x.htm

    The differences between C++03 and C++09 aren't yet finalized. You can track the work of the committee at http://www.open-std.org/JTC1/SC22/WG21/ - in particular, you can get the most recent draft of the standard at http://www.open-std.org/JTC1/SC22/WG...2007/n2369.pdf and the list of things to do in the Evolution group (the one responsible for core language additions) at http://www.open-std.org/JTC1/SC22/WG...007/n2336.html . The last of these is probably the best resource if you want to know what's coming.
    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. Anyone with a spare java class file(s) with nested classes?
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-29-2009, 08:33 PM
  2. Problem with nested classes
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 05:52 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  5. nested classes...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-09-2001, 04:25 PM