So, I have a base class and a child class. Something like
Class Device
{
double read();
}
Class Relay : Public Device
{
void toggleSwitch();
}
Class Sensor : Public Device
{
int getValue();
}
I have an array to store the objects so I can keep track of them
Device *devices[10];
devices[0] = new Relay();
devices[1] = new Sensor();
etc…
devices[0]->toggleSwitch(); // error
I can call the member read() from any of these devices fine since read() is in the base class. So how do I call toggleSwitch() in Relay? I would get an error saying 'class Device' has no member named 'toggleSwith'. It makes sense since the 'devices' array is of Device. So how do I call a member of the child? I tried casting but it wasn't having it.
thanks.
mistergreen:
yeah, it is complicated.
I want to make it simple for people to use so obviously, the code has to be complicated.
As long as it doesn't crash, I'm cool with it.
There is another facet of simplicity that seems important to me, especially in the DIY Arduino environment where many users have little experience of programming. It is this - will the user be easily able to understand the code if s/he needs to fix it or modify it?
mistergreen:
I can call the member read() from any of these devices fine since read() is in the base class. So how do I call toggleSwitch() in Relay? I would get an error saying 'class Device' has no member named 'toggleSwith'. It makes sense since the 'devices' array is of Device. So how do I call a member of the child? I tried casting but it wasn't having it.
thanks.
You can cast it, but you need to know whether a given array entry is actually an instance of the class you're interested in. One way to do that is to hard-code knowledge of which array entry is which class. I think that's a very bad way.
Another way to do it is for the base class to provide 'do nothing' implementations for all the interfaces so that you can call any method on any object, and have each subclass override the methods that apply to it. That's less ugly but still ugly.
A better way is to have some scheme for knowing that you are dealing with a relay before you try to do relay-like things to it, for example using RTTI, or by maintaining a separate index of items of each subclass..
mistergreen:
They won't even need to touch the code. It's all web based by configuring settings.
You mustn't read all that many threads here ...
...R
heh, it's working for generic devices that use digital, pwm, and analog in pins. For serial, i2c, or any means, the'd have to write their own class. I'll make sure to make instructions. It works, for reals.
I don't for a moment doubt that it works. The writers of various libraries that caused problems for users also believed that their code was perfect.
"You can't make anything foolproof because fools are so inventive"
Rest assured, if there is any uptake of your project somebody will try to use it in some way that won't work in their situation and the ability to understand the code and make a small modification would probably solve their problem.
Robin2:
I don't for a moment doubt that it works. The writers of various libraries that caused problems for users also believed that their code was perfect.
"You can't make anything foolproof because fools are so inventive"
Rest assured, if there is any uptake of your project somebody will try to use it in some way that won't work in their situation and the ability to understand the code and make a small modification would probably solve their problem.
...R
Well yeah, people will try to mod it. It should be pretty straight forward for experienced programmers. They'll probably see mistakes I couldn't . This is built for beginners. I wouldn't expect beginners to mess with the code.