473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Static nested class

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.
Jul 22 '05 #1
2 12819
"newbiecpp" <ne*******@yahoo.com> 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
Jul 22 '05 #2
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" <v.********@comAcast.net> wrote in message
news:MhzTc.252867$a24.141615@attbi_s03...
"newbiecpp" <ne*******@yahoo.com> 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

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ryan Stewart | last post by:
Why would you use a static nested class? If it can basically be treated and instantiated just like a top-level class, what's the point?
4
by: Vedanta Barooah | last post by:
greetings.... in a python nested class is it possible to change the value of the parent class's variable without actually creating an instance of the parent class, consider this code: class...
7
by: newbiecpp | last post by:
Why this code cannot be compiled (under VC++ 7.0): class Outer { public: class Inner { public: Inner() : in_data(0) {} void action() { Outer::out_data++; // illegal reference to...
12
by: Mortos | last post by:
I need some quick advice. I just need to reference a containing class from the nested class. I'm familiar with Java but C# is proving tricky. public BigClass { public int ID_BigClass = -99; ...
4
by: UncleJoe | last post by:
Hi all, What is the syntax to declare a nested class that can only be instantiated by the outer class, yet visible to the other classes? Essentially I want to limit the instantiation and access...
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
1
by: nabil | last post by:
Hi I have probleam while while initilazing nested class ... I needs to create a object 'b' of B(nested class) in A(surrounding class). while constructing b I have to pass pointer of funtion...
4
by: =?Utf-8?B?YmlsbCB0aWU=?= | last post by:
I'm perusing a class that seems to utilize a nested class for private fields. In a nutshell, it looks as follows: public class Foo { public Property1 { get/set Bar.field1 }
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.