-
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
-
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
-
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
-
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
-
Replies: 27
Last Post: 02-11-2016, 06:29 AM
-
By Osiris43 in forum .NET
Replies: 1
Last Post: 08-04-2006, 12:15 PM
-
Replies: 5
Last Post: 01-15-2006, 08:10 PM
-
Replies: 2
Last Post: 05-24-2005, 06:56 AM
-
Replies: 5
Last Post: 10-17-2002, 01: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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|