Skip to content

add constructor without arguments #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rolgordijn
Copy link
Contributor

i tried to pass an instance of MCP23017 by reference to the constructor of another class.
Doing this seems to require a constructor without arguments. I added the constructor without arguments with this change.

this is my own constructor; it is a wrapper class so i can create objects for each pin seperately and it inherits from my base class so i can make abstractions of different kinds of IO expanders, pins on the arduino itself, ...

MCP23017IO::MCP23017IO(MCP23017 &ic, uint8_t pin) : IO() { pin = pin; ic = ic; }

@blemasle
Copy link
Owner

blemasle commented Aug 30, 2024

Hi,

Sorry, I'm not sure to understand the issue correctly. Passing an argument by reference should not require a default constructor as the whole point to pass a reference is to avoid creating a new instance (and hence, it does not use the constructor).

Such code compiles without issue on my side

#include <Wire.h>
#include <MCP23017.h>

class Wrapper
{
  private:
    MCP23017* _mcp;

  public:
    Wrapper(MCP23017& mcp) {
      _mcp  = &mcp;
    }

    void init() {
      _mcp->init();
    }
};

#define MCP23017_ADDR 0x20
MCP23017 mcp = MCP23017(MCP23017_ADDR);
Wrapper wrapper = Wrapper(mcp);

void setup() {
    wrapper.init();
}

void loop() {
    
}

Do you have a full example I can try ?

@rolgordijn
Copy link
Contributor Author

In your example, you’re storing a pointer to the MCP23017 object within the Wrapper class.
Correct me if i'm wrong, but it should be



class Wrapper
{
  private:
    **MCP23017 _mcp;**

  public:
    Wrapper(MCP23017& mcp) {
      **_mcp  = mcp;**
    }

    void init() {
      _mcp->init();
    }
};

#define MCP23017_ADDR 0x20
MCP23017 mcp = MCP23017(MCP23017_ADDR);
Wrapper wrapper = Wrapper(mcp);

void setup() {
    wrapper.init();
}

void loop() {
    
}

My reasoning is that since the pin I’m controlling is hardwired to the MCP23017 chip on the PCB, the relationship between the MCP23017 object and the pin should be immutable. Using a reference ensures that this relationship remains fixed and cannot inadvertently be changed to point to a different instance or memory location, which is possible with a pointer.

@blemasle
Copy link
Owner

I did use a pointer because you usually want to avoid copies of objects which on an embedded platform are often considered wasted memory space.

be changed to point to a different instance

As long as your private pointer is not changed (and as it's private it can only be changed by your wrapper) that reference cannot be changed externally. You're pointer "outside" could be re-assigned to somewhere else but it won't change the value (or reference) of the pointer in your wrapper class.

Before integrating your change, I just want to be sure that you need to do this for what is it you're looking for. Because a default constructor would probably means using default values for one, and because I'm not quite sure you need a default constructor when what you seem to be looking is making a copy of the object (which should be added by the compiler automatically if I'm not mistaken). And if it is so, just pass the object not the object reference 🙂

@blemasle
Copy link
Owner

@rolgordijn I just saw that I didn't update the library.properties along with the github tag and that the last version available on arduino is 2.0.0. Since then, there is another constructor with some default parameter that make the thing compiles with what you want, I guess because the parameters are all specified so this can act as a default constructor.

Could you try to manually update the files and tells me if that works for you ? If so, I'll publish a new version matching the last tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants