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

OOP rules for global objects

Hi,

What I do in my current project:

1- I've developed a library by using C++ (compiler: arm-none-eabi-g++ -std=c++17) and generated a static library (arm-none-eabi-ar) with some c-api wrapper functions. In the static library there are some global instances of some classes, and exposed c functions provide access to their functionality

2- I've developed the app (compiler: arm-none-eabi-gcc) to be run on an ARM chip and linked with the static library. In the main function, a call is made to an init function which is in the static library

My observation is that, if I am not wrong, OOP inheritance rules don't work on those global objects. If I take a global object into a member function as a local variable, then I can access it via a pointer of its base class. A sample code is below:

Some test classes:

class CtBase
{
public:
    virtual void foo(void) = 0;
    int i = 0;
};

class CtDer : public CtBase
{
public:
    void foo(void) override;
};

void CtDer::foo(void)
{
    ++this->i;
}

 

Use them in a class member function implementation, where its instance (PlatformDevice) is a global object in the static library:

Version 1 - Doesn't work

CtDer der;

namespace mevoo::loni6
{

    void PlatformDevice::init(UdpComm *udp, BufferBase *b)
    {
        CtBase *ctptr = &der;

        ctptr->foo(); // doesn't work
// some more code

Version 2 - works:

namespace mevoo::loni6
{

    void PlatformDevice::init(UdpComm *udp, BufferBase *b)
    {
        CtDer der; // now a local var
        CtBase *ctptr = &der;

        ctptr->foo(); // works

// some more code

Any comment would be highly appreciated. 

Best regards,

Ozan

Parents
  • Update: I am sorry, I think it is not about OOP rules. Any pointer, no matter the type, to a global instance seems not to work. Please see the example:

    CtDer der;
    
    namespace mevoo::loni6
    {
    
        void PlatformDevice::init(UdpComm *udp, BufferBase *b)
        {
            CtDer *ctptr = &der;
            // CtBase *ctptr = &der;
    
            ctptr->foo(); // doesn't work
    
    // more code

    Don't you think this is very odd? Or am I doing something wrong? I am out of any logical answer. It (CtDer* ctptr) is simply a pointer of the same type, but a call to member function causes the app crash. 

Reply
  • Update: I am sorry, I think it is not about OOP rules. Any pointer, no matter the type, to a global instance seems not to work. Please see the example:

    CtDer der;
    
    namespace mevoo::loni6
    {
    
        void PlatformDevice::init(UdpComm *udp, BufferBase *b)
        {
            CtDer *ctptr = &der;
            // CtBase *ctptr = &der;
    
            ctptr->foo(); // doesn't work
    
    // more code

    Don't you think this is very odd? Or am I doing something wrong? I am out of any logical answer. It (CtDer* ctptr) is simply a pointer of the same type, but a call to member function causes the app crash. 

Children
No data