-
Notifications
You must be signed in to change notification settings - Fork 38
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
base: master
Are you sure you want to change the base?
Conversation
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 ? |
In your example, you’re storing a pointer to the MCP23017 object within the Wrapper class.
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. |
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.
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 🙂 |
@rolgordijn I just saw that I didn't update the 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. |
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; }