Static nested class

Discussion in 'C++' started by newbiecpp, Aug 15, 2004.

  1. newbiecpp

    newbiecpp Guest

    Java can declare a static nested class. Does C++ have same thing like?

    class Outer {
    public:
    static class Inner {
    ...
    };

    ....
    };

    I can compile this code. But my question is what's difference between Java
    and C++ in term of static nested class? In which case, it is good design to
    use static nested class?

    Thanks in advance.
    newbiecpp, Aug 15, 2004
    #1
    1. Advertising

  2. "newbiecpp" <> wrote...
    > Java can declare a static nested class. Does C++ have same thing like?
    >
    > class Outer {
    > public:
    > static class Inner {
    > ...
    > };
    >
    > ...
    > };
    >
    > I can compile this code. But my question is what's difference between

    Java
    > and C++ in term of static nested class? In which case, it is good design

    to
    > use static nested class?


    In Java, when you create a nested class, its instance is automatically
    created and added to the object or to the class (if the nested class is
    declared static), at least that's how I remember it. In C++ when you
    declare a nested class, you only define a type, there is no instance
    involved. In order to declare/define an instance, you need to explicitly
    do that:

    class Outer {
    public:
    class Inner {
    ...
    };

    Inner non_static_member;
    static Inner static_member;
    };

    Hence, there is no need to declare the type 'static', you do that when
    you declare the actual data member.

    So, the answer could be "no, C++ can't do that, there are no static
    types only static objects", or, if you prefer, "yes, C++ can do that,
    you just have to declare a static data member of the type Inner in the
    'Outer'". Pick whichever answer you like better.

    Victor
    Victor Bazarov, Aug 15, 2004
    #2
    1. Advertising

  3. newbiecpp

    È«±æµ¿ Guest

    In java, the static inner class has different meaning..

    Every java class members (include inner classes) must be accessed by object,
    not class.

    Only static members can be accessed by class.

    so, if you want access inner class without creation of object, you must
    declare as "static inner class".

    for example ..

    public class Outer {
    public class Inner {
    };
    public static class StaticInner {
    };
    public static void main(String[] args) {
    Outer.Inner x = new Outer.Inner <-- invalid
    Outer.StaticInner x = new Outer.StaticInner <-- valid
    }
    };



    "Victor Bazarov" <> wrote in message
    news:MhzTc.252867$a24.141615@attbi_s03...
    > "newbiecpp" <> wrote...
    >> Java can declare a static nested class. Does C++ have same thing like?
    >>
    >> class Outer {
    >> public:
    >> static class Inner {
    >> ...
    >> };
    >>
    >> ...
    >> };
    >>
    >> I can compile this code. But my question is what's difference between

    > Java
    >> and C++ in term of static nested class? In which case, it is good design

    > to
    >> use static nested class?

    >
    > In Java, when you create a nested class, its instance is automatically
    > created and added to the object or to the class (if the nested class is
    > declared static), at least that's how I remember it. In C++ when you
    > declare a nested class, you only define a type, there is no instance
    > involved. In order to declare/define an instance, you need to explicitly
    > do that:
    >
    > class Outer {
    > public:
    > class Inner {
    > ...
    > };
    >
    > Inner non_static_member;
    > static Inner static_member;
    > };
    >
    > Hence, there is no need to declare the type 'static', you do that when
    > you declare the actual data member.
    >
    > So, the answer could be "no, C++ can't do that, there are no static
    > types only static objects", or, if you prefer, "yes, C++ can do that,
    > you just have to declare a static data member of the type Inner in the
    > 'Outer'". Pick whichever answer you like better.
    >
    > Victor
    >
    >
    È«±æµ¿, Oct 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,358
    Tony Morris
    Oct 19, 2004
  2. E11
    Replies:
    1
    Views:
    4,707
    Thomas Weidenfeller
    Oct 12, 2005
  3. bugbear
    Replies:
    4
    Views:
    421
    bugbear
    Aug 23, 2006
  4. request@no_spam.com
    Replies:
    5
    Views:
    411
  5. Robin
    Replies:
    0
    Views:
    404
    Robin
    Jun 6, 2007
Loading...

Share This Page