Archived

This topic is now archived and is closed to further replies.

Inner Classes

This topic is 6033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
jesterlecodeur example of nested classes is a hack - it''s a good example of nested classes - but a bad example of how to implement COM coclasses, IMHO.
And it''s in an article on MSDN from the early days of COM...

If you were developing with Delphi or VB it''s how you would have to do it, but it''s not neccessary (and adds little benefit) in C++.

I guess jesterlecodeur''s example is a little different because the nested classes will be entirely different objects (and potentially more than one of them). Not a bunch of objects behaving like one object by using COM aggregation support.

Magmai Kai Holmlor
- The disgruntled & disillusioned

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Magmai Kai Holmlor, I think in Europe the French and Indian war was called the 7 years war(or similar), because it was not just fought in America but 2 other continents.

Share this post


Link to post
Share on other sites
Magmai Kai Holmlor, I think in Europe the French and Indian war was called the 7 years war(or similar), because it was not just fought in America but 2 other continents.

You are talking Indochine war and Algérie war maybe.

Why English rules?? C pas très malin tout ça!

Share this post


Link to post
Share on other sites
Thanks for the responsed so far, but I still don''t get what Magmai''s onto . Here''s what I think I understand from your post.

It is a hack because he''s making the nested classes a friend class, instead of defining the inner class within the outer class. Thus in effect he is actually making up a class out of several classes, which isn''t a good idea because you want the implementation of the inner class hidden. Is that right or am I way off base?

I use Delphi and a little VB, and I think that jester''s technique is necessary because there isn''t proper support for inner classes in those languages.


Anyway, you guys have been pretty helpful. I''ve been pouring over material on inner classes and writing test code. They''re starting to become clearer (albeit slightly).

Share this post


Link to post
Share on other sites