Inner Classes

Started by
12 comments, last by NuffSaid 22 years, 10 months ago
I''ve read up on inner classes, but for the life of me, I don''t see why they''re needed. Could anyone explain to me why I would ever need an inner class? The examples I''ve come across (Thinking in Java 2nd Ed) don''t really convince me they''re necessary. Any help will be appreciated.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
Hi,

"I''ve read up on inner classes, but for the life of me, I don''t see why they''re needed.
Could anyone explain to me why I would ever need an inner class? The examples I''ve come across (Thinking in Java 2nd Ed) don''t really convince me they''re necessary."

First of all there is normally no mechanism that will ever really physically force you to choose inner classes, but sometimes it makes sense. Lets assume you write a class which needs some helper class that needs knowledge about the outer class attributes and methods, then you would normally use inner classes for example in JAVA. (Be careful to compare JAVA and C++ at this point, because they have a different behaviour. While JAVA inner classes can easily access everything that is wrapped around them, this is not automatically true for inner C++ classes.)

An example is that you may write a linked list class, then you will have a class that describes a node in your list. It makes sense to use an inner class for the node representation and do not give the implementation of this inner class to the outside. Doing so will prevent you from chaos if you decide to change the inner class (node) implementation later on. All a user of your list is interested in, is to iterate over your list and manipulate the list and its contents. This should be done with safe iterators and using abstract interfaces. Your list class should have the standard methods like create, delete, changeValue, etc. so that there is no need to explicitly create a node outside in the list users code and then add it to the inner guts of your list class. All new instances of nodes should be implicitly created inside your list class by using the public methods of your list class. Using the appropriate keywords (private, protected) for the constructors of the inner class will make it impossible to instance such a inner class from the outside. The reason to do so is "encapsulation of data" which grants the consistence of data.

cu

Peter
STL''s iterators are a good implementation of inner classes as well.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Peter:

Thanks for the info. The linked-list example does make sense. But the way I implement my linked list classes is by using composition. I define a List class and a separate Node class.

  class Node{//..};class List{//..  Node head;};  


In time, I might find inner classes useful, but at the moment, it doesn''t really make much sense to me. Thanks for your help.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
I do it just for tidyness... If I want a class to be accessed only within that class, then it makes sense to contain it. It also prevents any later misunderstandings about what owns what, and where certain classes should be used. Though certainly some of my inner classes get messy... try this

CLandMap
|- CRegion
_ |- CLayer
_ | |- PointRef_t
_ | |- TriangleRef_t
_ |- RegionSize_t

Keeps me honest though


Edited by - ludemann8 on June 8, 2001 10:00:15 AM
It is used in the COM mecanish for multiple interface.

for exemple:
  class CODXGraphics : public IUnknown{  // This class implement IUnkown methods  // and can have internal methods for used by other interfaces  class CODirect3D: public IDirect3D    // This class implement IDirect3D    // and calls CODXGraphics methods & properties  friend CODirect3D;  CODirect3D  m_D3D; // The instance  class CODirect3DDevice: public IDirect3DDevice    // This class implement IDirect3DDevice    // and calls CODXGraphics methods & properties     friend CODirect3DDevice;  CODirect3DDevice m_Device; // The instance}So you can call each interfaces by QueryInterface  


Why English rules?? C pas très malin tout ça!
_______________
Jester, studient programmerThe Jester Home in French
NOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
That''s a hack for SI systems! (Like VB, Delphi, you-name-it)
A perfect example of the power of MI is COM interfaces & the ATL.

As posted above, you just need a friend cluase to make C++ nested classes act like a Java inner classes - unless there''s another difference I don''t know.

Why have directories? Why have clases? Why have namespaces? Kinda the same reason to have nested classes & nested namespaces.


Why English rules? probably $''s
If the Russian had won the cold-war & built the Internet... If Japan hadn''t adopted English.... If the French won the French & Indian War (I wonder what it''s called in France?)... well we''ll never know.

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
A hack or not it''s in the MSDN.
I think ATL doesn''t make very differents.


Why English rules?? C pas très malin tout ça!
_______________
Jester, studient programmerThe Jester Home in French
Magmai:
I don''t use ATL or COM. I did use some MFC. Sorry, but I''m lost as to what you are calling a hack.

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
I use java annoymous inner classes for things like menus and window close events. Since each MenuItem should do something different you either need to be able to parameterize some ActionListener subclass using Method objects (which are so ugly) or you can simply use inner classes.

For example I want a menu item that when selected calls a method:

MenuItem file1 = new MenuItem("save");
file1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{Global.save();}
});

so I''m creating a class that doesn''t actually have a name, and I don''t even need to use the class keyword. Since the definition follows new ActionListener() it automatically implements ActionListener, so I override the method and that''s it. As for the named inner classes, I haven''t really figured them out yet so I can''t help you with that.

This topic is closed to new replies.

Advertisement