Re: how to organize several class and their instance in windows programming
"asm23" <asmwarrior@gmail.com> ha scritto nel messaggio
news:g15gls$tdm$1@aioe.org...
class CMainFrame : public CFrameWnd{
......
public:
CImageProc m_image;
CMotionControl m_motor;
CNetwork m_socket;
CXXXClass m_myxxx;
...
}
So, I'm confused that how does these members communicate with each other.
For example, After Image processing, I would like to do some motion
control...
[...]
You can imagine that things become more complicated while all these
Classes will Call other class's function. Each Class should maintain
several Pointers pointing to other classes.
You may consider defining a class that stores instances of those "shared"
"component" classes, e.g.
// Stores all the subsystems of your app
// (image processing, motion control, ...)
class CSystemCore
{
public:
CImageProc m_image;
CMotionControl m_motor;
CNetwork m_socket;
...
//
// Of course you may declare the above instances
// as private/protected, and just expose some
// public methods for accessing them, e.g.
//
// private:
// CImageProc m_image;
//
// public:
// CImageProc & GetImageProc() { return m_image; }
//
};
Then, you can pass a pointer to the CSystemCore instance to each of the
"component" classes (like CImageProc, etc.), e.g.
// Forward reference
class CSystemCore;
CImageProc
{
...
// Needs to access to all system components
CSystemCore * m_pSystem;
};
So, inside your CImageProc methods, you can reference the other system
components (like CMotionControl), using the pointer to CSystemCore, e.g.
void CImageProc::DoSomething()
{
...
// Need to control the m_motor CMotionControl instance
m_pSystem->m_motor.DoSomethingWithMotor(...);
// or more elegant (with private instance of CMotionControl
// and public accessor):
// m_pSystem.GetMotionControl().DoSomethingWithMotore(...);
}
Of course, when you create the CSystemCore instance, you must set the
pointer to it into several component classes, e.g.
// In CSystemCore constructor
CSystemCore::CSystemCore()
{
m_image.m_pSystem = this;
m_motor.m_pSystem = this;
}
Or you can define proper constructors for CImageProc, CMotionContorl, etc.
CImageProc( SystemCore * pSystem )
: m_pSystem( pSystem )
...
{
...
}
CMotionControl( SystemCore * pSystem )
: m_pSystem( pSystem )
...
{
...
}
And initialize these instances in CSystemCore constructor like this:
CSystemCore::CSystemCore()
: m_image( this ),
m_motor( this ),
...
{
...
}
So, you can make CSystemCore a singleton in your app, or you can put it into
your CDocument class, or in whatever place your design suggests.
HTH,
Giovanni