This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ARM Compiner c++ error (class, virtual, override)

Hi

When I try to make new class, which inherited from another class I found, as I understand, a mistake in working a compiled code.

Code Example:

char s[1024];

class Parent{
        public:
                Parent(){
                        vMethod();
                };
                virtual void vMethod(){
                        sprintf(s,"Parent virtual method");
                };
};

class Child: Parent{
        public:
                Child(){
                        vMethod();
                };
                virtual void vMethod(){
                        sprintf(s,"Child virtual method");
                };
};

int main(void){

        Child* c = new Child();
...

So, what happends. At this code the methos and constructor calling should to be the next:
1. Calling the Parent constructor, which call overrided vMethod of Child class
2. Calling the Child constructor, which again call overrided vMethod of Child class.

But the Parent construcor calls Parent vMethod and the Child constructor calls Child vMethod. So clasic overriding is works not correct.

The clasic construction on overrided method shoilt to looks like:
override void vMethod(){}
or
virtual void vMethod() override{}
but none of this code construction dosn't work.

So it's a error in compilator or I shoult to use another construction for correct overriding virtual class methods.

Thanks.

Parents

  • My aim was to make a basic class and functional in it and override some methods in child with adding functionality.

    Sure again. You once again cannot do that with constructors.

    If you actually think about it, you should be able to convince yourself that it cannot work in all cases. (especially ones that require child class initialization)

    I gust show a code which I try to use an I think you understand an idea. Maybe its more difficult then need for this task, but its beautiful in my opinion

    template <class T>
    class Driver
    {
            private:
                    static SemaphoreHandle_t busy_smphr;
            public:
                    ~Driver<T> ();
                    static T* Open(TickType_t xTicksToWait);
                    virtual void Close();
    };
    
    template <class T>
    SemaphoreHandle_t Driver<T>::busy_smphr = xSemaphoreCreateMutex();
    
    template <class T>
    Driver<T>::~Driver<T> ()
    {
            Close();
    };
    
    template <class T>
    T* Driver<T>::Open(TickType_t xTicksToWait)
    {
            if (xSemaphoreTake(Driver<T>::busy_smphr, xTicksToWait)==pdTRUE)
               return new T();
            else
               return NULL;
    };
    
    template <class T>
    void Driver<T>::Close()
    {
        xSemaphoreGive(T::busy_smphr);
    };
    

    Ans the child class
    NewDevice.h

    class NewDevice: public Driver<NewDevice>
    {
      public:
            static NewDevice* Open(TickType_t xTicksToWait);
            static void Init(void);
            void Method();
            virtual void Close();
    };
    

    NewDevice.cpp

    
    NewDevice* NewDevice::Open(TickType_t xTicksToWait)
    {
            NewDevice* husart = Driver<NewDevice>::Open(xTicksToWait);
            if (husart!=NULL){
                    ...
            }
            return husart;
    }
    
    void NewDevice::Close() {
            Driver<NewDevice>::Close();
    }
    
    void NewDevice::Method() {
            ...
    }
    

    And using in code

    NewDevice::Init();
    NewDevice* drv = NewDevice::Open(10);
    if (drv!=NULL)
       drv->Method();
    delete drv;
    

    On deleting object calls destructor from parent, which should to call new overriden Close(), which should to call previous Close(). But its doesn't work. I found a slution, but its not so beautiful.

    Thanks for your answers.

Reply

  • My aim was to make a basic class and functional in it and override some methods in child with adding functionality.

    Sure again. You once again cannot do that with constructors.

    If you actually think about it, you should be able to convince yourself that it cannot work in all cases. (especially ones that require child class initialization)

    I gust show a code which I try to use an I think you understand an idea. Maybe its more difficult then need for this task, but its beautiful in my opinion

    template <class T>
    class Driver
    {
            private:
                    static SemaphoreHandle_t busy_smphr;
            public:
                    ~Driver<T> ();
                    static T* Open(TickType_t xTicksToWait);
                    virtual void Close();
    };
    
    template <class T>
    SemaphoreHandle_t Driver<T>::busy_smphr = xSemaphoreCreateMutex();
    
    template <class T>
    Driver<T>::~Driver<T> ()
    {
            Close();
    };
    
    template <class T>
    T* Driver<T>::Open(TickType_t xTicksToWait)
    {
            if (xSemaphoreTake(Driver<T>::busy_smphr, xTicksToWait)==pdTRUE)
               return new T();
            else
               return NULL;
    };
    
    template <class T>
    void Driver<T>::Close()
    {
        xSemaphoreGive(T::busy_smphr);
    };
    

    Ans the child class
    NewDevice.h

    class NewDevice: public Driver<NewDevice>
    {
      public:
            static NewDevice* Open(TickType_t xTicksToWait);
            static void Init(void);
            void Method();
            virtual void Close();
    };
    

    NewDevice.cpp

    
    NewDevice* NewDevice::Open(TickType_t xTicksToWait)
    {
            NewDevice* husart = Driver<NewDevice>::Open(xTicksToWait);
            if (husart!=NULL){
                    ...
            }
            return husart;
    }
    
    void NewDevice::Close() {
            Driver<NewDevice>::Close();
    }
    
    void NewDevice::Method() {
            ...
    }
    

    And using in code

    NewDevice::Init();
    NewDevice* drv = NewDevice::Open(10);
    if (drv!=NULL)
       drv->Method();
    delete drv;
    

    On deleting object calls destructor from parent, which should to call new overriden Close(), which should to call previous Close(). But its doesn't work. I found a slution, but its not so beautiful.

    Thanks for your answers.

Children