Defining a class-member inside of a class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Defining a class-member inside of a class

  1. #1
    Join Date
    Jun 2005
    Posts
    20

    Defining a class-member inside of a class

    I have a class that should have a class-member.
    I may define class to be member outside of the class like

    class MC
    {
    public:
    double w;
    double h;
    };

    class A
    {
    .........
    public:
    clas MC mc;
    };

    or define it inside the class A:

    class A
    {
    public:
    class MC
    {
    public:
    double w;
    double h;
    };
    ...............
    public:
    class MC mc;
    };

    For now only class A is using class MC.

    What are pros and contras of the first and second version and what is the best programming practices?

    Thank you

    geoyar

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Defining a class inside a class is not very common. When you define a class inside another class, the inner class is usually a helper class that is used only by the enclosing class. I rarely use this technique though since it complicate the design, especially when working with templates.

    Having a member object is so widely-used that you can see it almost everywhere: a class that has a string object, a file stream object, a date object etc.
    In these cases you will often observe that the enclosing class has nothing to do with the embedded data object. For example, an employee class has nothing to do with string, so you certainly won't define a string class inside employee -- instead, you would use one or more string objects as members of class employee. The main benefit here is that every class can declare object members of this type: employee can have string objects as members, class MP3_clip can have a string member etc.
    Danny Kalev

  3. #3
    Join Date
    Jun 2005
    Posts
    20
    I agree that the defining a class inside a class complicates design. But recently I read two articles on www.codeproject.com, 'Brainnet 2 ...' By An 'OOP' and 'Neural Network for Recognition of Handwritten Digits' By Mike O'Neill, and both authors use classes defined inside the other classes.
    E.g., Mike O'Neill writes:

    class CMNistDoc : public COleDocument
    {
    .....................................

    class CAutoCS
    {
    public:
    CAutoCS(CRITICAL_SECTION& rcs) : m_rcs(rcs)
    { ::EnterCriticalSection( &m_rcs ); }
    virtual ~CAutoCS() { ::LeaveCriticalSection( &m_rcs ); }
    private:
    CRITICAL_SECTION& m_rcs;
    }; // class CAutoCS
    };

    The class CMNistDoc does not have any member of type CAutoCS, but has a member CRITICAL_SECTION m_cs. Mike uses CAutoCS heavily in implementation of CMNistDoc to lock/unlock pieces of code, like

    CMNistDoc::MyFn()
    {
    ..................

    CAutoCS(m_cs);

    ..................
    }

    I do not see any gain from defining CAutoCS inside CCMNistDoc, but maybe I am not understanding something.

    geoyar

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    This is a frequently used technique in Java ... which also has anonymous classes. The scope of such a class is wholly within the scope of the containing class. A Node class for a List or Map or other such container is an example of a Class (or Struct), or a "Comparator" class which would be private to the container class the nodes relate to and could be defined within the definition of the "outer" class.

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 05:12 AM
  2. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  3. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  4. Replies: 2
    Last Post: 05-24-2005, 07:56 AM
  5. Replies: 5
    Last Post: 10-17-2002, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center