Home > Enterprise >  How can I access a private (non-static) method in C ?
How can I access a private (non-static) method in C ?

Time:10-18

Currently I am working on a project where I want to control a model train for a nice showcase.

I have multiple locomotives which all have a unique address (just think of it as a UUID). Some locomotives have a headlight, some of them have a flashing light, some have both and some of them have none.

My base class is this:

class GenericLocomotive : public Nameable, public Describable {

private:
    uint16_t address;

public:
    GenericLocomotive(const char* name, const char* description, uint16_t address);
    void setFunction(uint8_t command, bool val);

Now I want to have a different class which provides the functionality to enable and disable the headlight:

class HasHeadLight {

public:
    void activateHeadlight();
    void deactivateHeadlight();
}

My goal is to have a specific class for every locomotive (with different functionality) which looks something like this:

class <SpecificLocomotive> : public GenericLocomotive, public HasHeadlight, public HasFlashlight,... {
    ...
}

The problem is, that I must have access to the private field 'address' of my GenericLocomotive class and I also have to call the function setFunction(...) from my HasHeadlight class.

I am quite new to C and just found out about the concept of friend classes and methods, but I can not quite get it to work, because even with the declaration of the method setFunction(...) as a friend, I can not just call something like

this->setFunction(HEADLIGHT_COMMAND, true);

from my HasHeadlight-class, because the function is not declared in 'this'.

How can I access the method from my other class? Is this friend thing even needed or is there a completely different way to structure my C program?

CodePudding user response:

You have misunderstood how class inheritance works:

Inheritance establishes an is-a relationship between a parent and a child. The is-a relationship is typically stated as as a specialization relationship, i.e., child is-a parent.

There are many ways you can tackle what you want to achieve here, but this is not it. You're on the right track as far as treating the different train components as separate objects, and one way to achieve that would be to instead make each component a member of the specialized locomotive:

class HeadLight {
public:
    void activateHeadlight();
    void deactivateHeadlight();
}

class SpecialLocomotive : public GenericLocomotive {
    HeadLight mHeadlight;
    Flashlight mFlashlight;

public:
    SpecialLocomotive(const char* name, const char* description, uint16_t address)
    :    GenericLocomotive(name, description, address) {
        setFunction(HEADLIGHT_COMMAND, true);
    }

    void toggleLight(bool on) {
        if (on) {
            mHeadlight.activateHeadlight();
        } else {
            mHeadlight.void deactivateHeadlight();
        }
    }
    /* so on and so forth /*
}
    

There's not enough details to go further with it. If you need to call setFunction from within Headlight, I would consider that a poor design choice, but there are other ways.

  • Related