Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using New Opperator for object pointers ( Polymorphism )

using New Opperator for object pointers ( Polymorphism )

Scheduled Pinned Locked Moved C / C++ / MFC
wpfoop
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ScotDolan
    wrote on last edited by
    #1

    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

    L M 2 Replies Last reply
    0
    • S ScotDolan

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • L 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

        S Offline
        S Offline
        ScotDolan
        wrote on last edited by
        #3

        It's not a easy simple C++ Question. Scott

        Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

        L 1 Reply Last reply
        0
        • S ScotDolan

          It's not a easy simple C++ Question. Scott

          Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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)

          1 Reply Last reply
          0
          • S ScotDolan

            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

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • M Mark Salsbery

              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

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Mark Salsbery wrote:

              See what I've done here?

              No, but I can picture you flopping all around inside his boat. ;P

              led mike

              M 1 Reply Last reply
              0
              • L led mike

                Mark Salsbery wrote:

                See what I've done here?

                No, but I can picture you flopping all around inside his boat. ;P

                led mike

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                ....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:

                L 1 Reply Last reply
                0
                • M Mark Salsbery

                  ....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:

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  Mark Salsbery wrote:

                  We need a fish smiley!

                  That would be so great, I'd ask Chris for one if I thought there was any chance of getting it.

                  led mike

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups