using New Opperator for object pointers ( Polymorphism )
-
I have two different object CLX_MITSUBISHI_POLLING and CLX_LENZE_POLLING. These two classes both inherit four other classes that are similar but not identical. The functions names and parameter of the different inherited classes are identical but the code is not. I am have difficultly finding a way to create a generic class object pointer and then useing the new operator. I am finding it difficult because, i don't have a way to create a generic base class because of the four other objects inherited(CLX_LENZE_LIFT_INVERTOR,etc.) I was thinking may i could do something with templates or null pointer( void *inverter; inverter = new CLX_MITSUBISHI_POLLING();) Unfortunately, the null pointer did not work. Any recommendations would be greatly a appreciated. Cheers Scott
CLX_POLLING *Invertors; if( inverter_type == MITSUBISHI ) Invertors = new CLX_MITSUBISHI_POLLING; else Invertors = new CLX_LENZE_POLLING;
My CLX_MITSUBISHI_POLLING clases#pragma once #include "windows.h" #include "ClxThread.h" #include "ClxSafeQue.h" #include "ClxMitsubishiParameters.h" #include "ClxMitsubishiLiftInvertor.h" #include "ClxMitsubishiPitchInvertor.h" #include "ClxMitsubishiRollInvertor.h" #include "ClxMitsubishiCounterWeightInvertor.h" class CLX_MITSUBISHI_POLLING : public ClxThread, public CLX_MITSUBISHI_PITCH_INVERTOR, public CLX_MITSUBISHI_ROLL_INVERTOR, public CLX_MITSUBISHI_LIFT_INVERTOR, public CLX_MITSUBISHI_COUNTERWEIGHT_INVERTOR { public: CLX_MITSUBISHI_POLLING(void); ~CLX_MITSUBISHI_POLLING(void); bool mStartPolling(void){ return CreateNewThread(); } private: // Serial Port Specific Commands and Data unsigned short m_BaudRate; int m_CommPort; int m_SerialPortHandlePolling; public: bool mvInitComm( int comm_port, int baud_rate ); bool mvCloseComm( void ); bool mvRestartComm( void ); void mvSetBaudRate(int value); unsigned short mvGetBaudRate(void); void mvSetCommPort(int value); unsigned short mvGetCommPort(void); int mvGetSerialPortHandle( void ); void mvSetSerialPortHandle( int ); private: int mvPutByte( char txdata ); int mvPutPacket( int lenght, char txdata[] ); int mvGetBytes( void ); int mvFlushRxBuf( void ); int mvFlushTxBuf( void ); int mvGetRxBuf(void); int mvGetTxBuf(void); bool mvIsOverrunErrors(void); bool mvIsParityErrors(void); bool
-
I have two different object CLX_MITSUBISHI_POLLING and CLX_LENZE_POLLING. These two classes both inherit four other classes that are similar but not identical. The functions names and parameter of the different inherited classes are identical but the code is not. I am have difficultly finding a way to create a generic class object pointer and then useing the new operator. I am finding it difficult because, i don't have a way to create a generic base class because of the four other objects inherited(CLX_LENZE_LIFT_INVERTOR,etc.) I was thinking may i could do something with templates or null pointer( void *inverter; inverter = new CLX_MITSUBISHI_POLLING();) Unfortunately, the null pointer did not work. Any recommendations would be greatly a appreciated. Cheers Scott
CLX_POLLING *Invertors; if( inverter_type == MITSUBISHI ) Invertors = new CLX_MITSUBISHI_POLLING; else Invertors = new CLX_LENZE_POLLING;
My CLX_MITSUBISHI_POLLING clases#pragma once #include "windows.h" #include "ClxThread.h" #include "ClxSafeQue.h" #include "ClxMitsubishiParameters.h" #include "ClxMitsubishiLiftInvertor.h" #include "ClxMitsubishiPitchInvertor.h" #include "ClxMitsubishiRollInvertor.h" #include "ClxMitsubishiCounterWeightInvertor.h" class CLX_MITSUBISHI_POLLING : public ClxThread, public CLX_MITSUBISHI_PITCH_INVERTOR, public CLX_MITSUBISHI_ROLL_INVERTOR, public CLX_MITSUBISHI_LIFT_INVERTOR, public CLX_MITSUBISHI_COUNTERWEIGHT_INVERTOR { public: CLX_MITSUBISHI_POLLING(void); ~CLX_MITSUBISHI_POLLING(void); bool mStartPolling(void){ return CreateNewThread(); } private: // Serial Port Specific Commands and Data unsigned short m_BaudRate; int m_CommPort; int m_SerialPortHandlePolling; public: bool mvInitComm( int comm_port, int baud_rate ); bool mvCloseComm( void ); bool mvRestartComm( void ); void mvSetBaudRate(int value); unsigned short mvGetBaudRate(void); void mvSetCommPort(int value); unsigned short mvGetCommPort(void); int mvGetSerialPortHandle( void ); void mvSetSerialPortHandle( int ); private: int mvPutByte( char txdata ); int mvPutPacket( int lenght, char txdata[] ); int mvGetBytes( void ); int mvFlushRxBuf( void ); int mvFlushTxBuf( void ); int mvGetRxBuf(void); int mvGetTxBuf(void); bool mvIsOverrunErrors(void); bool mvIsParityErrors(void); bool
Are you sure you provided the smallest amount of code required to understand your problem? From "How to get an answer to your question": #4 Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
led mike
-
Are you sure you provided the smallest amount of code required to understand your problem? From "How to get an answer to your question": #4 Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
led mike
-
It's not a easy simple C++ Question. Scott
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
You really should have thought all of this through in the design phase of the project. It can be quite difficult and confusing to fix these things midway through the project. At any rate you may be able to solve it with virtual inheritance. But I doubt anyone here at codeproject will be able to solve it just by looking at code snippits. See if the following ariticles give you some ideas. http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.11[^] http://www.phpcompiler.org/doc/virtualinheritance.html[^] http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=168[^] Best Wishes, -Randor (David Delaune)
-
I have two different object CLX_MITSUBISHI_POLLING and CLX_LENZE_POLLING. These two classes both inherit four other classes that are similar but not identical. The functions names and parameter of the different inherited classes are identical but the code is not. I am have difficultly finding a way to create a generic class object pointer and then useing the new operator. I am finding it difficult because, i don't have a way to create a generic base class because of the four other objects inherited(CLX_LENZE_LIFT_INVERTOR,etc.) I was thinking may i could do something with templates or null pointer( void *inverter; inverter = new CLX_MITSUBISHI_POLLING();) Unfortunately, the null pointer did not work. Any recommendations would be greatly a appreciated. Cheers Scott
CLX_POLLING *Invertors; if( inverter_type == MITSUBISHI ) Invertors = new CLX_MITSUBISHI_POLLING; else Invertors = new CLX_LENZE_POLLING;
My CLX_MITSUBISHI_POLLING clases#pragma once #include "windows.h" #include "ClxThread.h" #include "ClxSafeQue.h" #include "ClxMitsubishiParameters.h" #include "ClxMitsubishiLiftInvertor.h" #include "ClxMitsubishiPitchInvertor.h" #include "ClxMitsubishiRollInvertor.h" #include "ClxMitsubishiCounterWeightInvertor.h" class CLX_MITSUBISHI_POLLING : public ClxThread, public CLX_MITSUBISHI_PITCH_INVERTOR, public CLX_MITSUBISHI_ROLL_INVERTOR, public CLX_MITSUBISHI_LIFT_INVERTOR, public CLX_MITSUBISHI_COUNTERWEIGHT_INVERTOR { public: CLX_MITSUBISHI_POLLING(void); ~CLX_MITSUBISHI_POLLING(void); bool mStartPolling(void){ return CreateNewThread(); } private: // Serial Port Specific Commands and Data unsigned short m_BaudRate; int m_CommPort; int m_SerialPortHandlePolling; public: bool mvInitComm( int comm_port, int baud_rate ); bool mvCloseComm( void ); bool mvRestartComm( void ); void mvSetBaudRate(int value); unsigned short mvGetBaudRate(void); void mvSetCommPort(int value); unsigned short mvGetCommPort(void); int mvGetSerialPortHandle( void ); void mvSetSerialPortHandle( int ); private: int mvPutByte( char txdata ); int mvPutPacket( int lenght, char txdata[] ); int mvGetBytes( void ); int mvFlushRxBuf( void ); int mvFlushTxBuf( void ); int mvGetRxBuf(void); int mvGetTxBuf(void); bool mvIsOverrunErrors(void); bool mvIsParityErrors(void); bool
A shorter smaple of code would certainly make it easier for those of us not familiar with it to study :) I get the feeling that the CLX_xxxx_POLLING classes should have a common interface base class (a class with just the common methods pure virtual). These polling classes should only derive from this common polling base class. Instead of being derived from the thread and four invertor classes, the polling classes should contain objects of these classes. Ack maybe pseudo-code makes more sense...I just think you're over-naming yourself right out of object oriented potential here ;)...
// classes shortened for brevity...
class CLX_COMMON_INVERTER
{
//Common inverter data
ClxSafeQue<char> m_sqCmdQue;
CRITICAL_SECTION m_csLock;// Common inverter methods
virtual bool IsConnected( void ) = 0;
virtual bool IsInhibited( void ) = 0;
virtual bool IsActiveFault( void ) = 0;
virtual bool IsBootMode( void ) = 0;
};class CLX_COMMON_POLLING
{
// Common data
CLX_COMMON_INVERTER *pPitchInverter;
CLX_COMMON_INVERTER *pRollInverter;
CLX_COMMON_INVERTER *pLiftInverter;
CLX_COMMON_INVERTER *pCounterweightInverter;
unsigned short m_BaudRate;
int m_CommPort;
int m_SerialPortHandlePolling;// Common methods
virtual bool mvInitComm( int comm_port, int baud_rate ) = 0;
virtual bool mvCloseComm( void ) = 0;
virtual bool mvRestartComm( void ) = 0;
virtual void mvSetBaudRate(int value) = 0;
virtual unsigned short mvGetBaudRate(void) = 0;}
class CLX_MITSUBISHI_POLLING : public CLX_COMMON_POLLING
{
// Data specific to this derived class// Implement Common methods
virtual bool mvInitComm( int comm_port, int baud_rate );
virtual bool mvCloseComm( void );
virtual bool mvRestartComm( void );
virtual void mvSetBaudRate(int value);
virtual unsigned short mvGetBaudRate(void);
}class CLX_LENZE_POLLING : public CLX_COMMON_POLLING
{
// Data specific to this derived class// Implement Common methods
virtual bool mvInitComm( int comm_port, int baud_rate );
virtual bool mvCloseComm( void );
virtual bool mvRestartComm( void );
virtual void mvSetBaudRate(int value);
virtual unsigned sho -
A shorter smaple of code would certainly make it easier for those of us not familiar with it to study :) I get the feeling that the CLX_xxxx_POLLING classes should have a common interface base class (a class with just the common methods pure virtual). These polling classes should only derive from this common polling base class. Instead of being derived from the thread and four invertor classes, the polling classes should contain objects of these classes. Ack maybe pseudo-code makes more sense...I just think you're over-naming yourself right out of object oriented potential here ;)...
// classes shortened for brevity...
class CLX_COMMON_INVERTER
{
//Common inverter data
ClxSafeQue<char> m_sqCmdQue;
CRITICAL_SECTION m_csLock;// Common inverter methods
virtual bool IsConnected( void ) = 0;
virtual bool IsInhibited( void ) = 0;
virtual bool IsActiveFault( void ) = 0;
virtual bool IsBootMode( void ) = 0;
};class CLX_COMMON_POLLING
{
// Common data
CLX_COMMON_INVERTER *pPitchInverter;
CLX_COMMON_INVERTER *pRollInverter;
CLX_COMMON_INVERTER *pLiftInverter;
CLX_COMMON_INVERTER *pCounterweightInverter;
unsigned short m_BaudRate;
int m_CommPort;
int m_SerialPortHandlePolling;// Common methods
virtual bool mvInitComm( int comm_port, int baud_rate ) = 0;
virtual bool mvCloseComm( void ) = 0;
virtual bool mvRestartComm( void ) = 0;
virtual void mvSetBaudRate(int value) = 0;
virtual unsigned short mvGetBaudRate(void) = 0;}
class CLX_MITSUBISHI_POLLING : public CLX_COMMON_POLLING
{
// Data specific to this derived class// Implement Common methods
virtual bool mvInitComm( int comm_port, int baud_rate );
virtual bool mvCloseComm( void );
virtual bool mvRestartComm( void );
virtual void mvSetBaudRate(int value);
virtual unsigned short mvGetBaudRate(void);
}class CLX_LENZE_POLLING : public CLX_COMMON_POLLING
{
// Data specific to this derived class// Implement Common methods
virtual bool mvInitComm( int comm_port, int baud_rate );
virtual bool mvCloseComm( void );
virtual bool mvRestartComm( void );
virtual void mvSetBaudRate(int value);
virtual unsigned sho -
Mark Salsbery wrote:
See what I've done here?
No, but I can picture you flopping all around inside his boat. ;P
led mike
....gills flaring...gasping for any water to extract some O2 from...it's a sad sight... :sigh: We need a fish smiley! :laugh:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
....gills flaring...gasping for any water to extract some O2 from...it's a sad sight... :sigh: We need a fish smiley! :laugh:
Mark Salsbery Microsoft MVP - Visual C++ :java: