Have been getting back to C++ for the first time in ages and finding it hard to get all the context parts right, especially as Im working with a library that I don't fully understand. Any help would be much appreciated!
Basically I have a library which is interfacing with my alarm through serial and presenting data through a WiFi connected ESP8266. I am trying to display some of the data from this library in a JSON feed.
The library sets up a struct called Zone, and I want to access the various zoneNames inside.
struct Zone {
bool enrolled; //PowerMax knows about this zone (it's configured)
char name[0x11]; //Will be dowloaded from PowerMax eprom
unsigned char zoneType;
const char* zoneTypeStr;
unsigned char sensorId;
const char* sensorType;
const char* sensorMake;
unsigned char signalStrength; //0 - bad, 1 - poor, 2 - good, 3 - strong, 0xFF unknown
ZoneState stat; //basic state of the zone
ZoneEvent lastEvent; //last event recodred for this zone
unsigned long lastEventTime;
void DumpToJson(IOutput* outputStream);
};
Whenever the library refers to a zone name it calls zone[ x].name and then gets used in whatever way it is needed, however when I try pm.zone[ x].name it tells me "error: 'Zone PowerMaxAlarm::zone [31]' is protected". I have managed to override a few of the functions inside the library using:
class MyPowerMax : public PowerMaxAlarm
{
public:
virtual void OnStatusChange(const PlinkBuffer * Buff)
{
PowerMaxAlarm::OnStatusChange(Buff);
}
}
However, I am stuck for how to override/access the information in the Zones struct. I only need read access, just to print some information on a webpage, but Im struggling.
Please can somebody help me!
(class MyPowerMax : public PowerMaxAlarm { public:)
then every value/member of zone seems to return a null when I use pm.zone[ix].name. Most of them are just garbage, while a couple of the zones contain garbage.
Obviously I need it to copy/link it to the actual class or something like that?
Sorry, I just thought it was easier to paste it there where it is formatted in colour codes and the like (I've used it before with SQL when sharing other code and found it was helpful).
I was hoping I could work through it myself but I cant seem to get the struct's to match and hence had to ask for any advice. I guess I was more asking the question about how to access a protected variable outside of the CPP library where it is defined? I have managed it with accessing functions as I found some useful examples to replicate, but for the actual variables themselves I am lost. Maybe I should try and write a function in the override which returns the zone names? Or something like that? It would be easy to modify the library but I know that is not the right path to take!
Protected means you don't need to access that from outside the class. The class defines all the ways that you are allowed to access it and private and protected variables are internal implementation details that have no use outside the class. By making it protected the original programner is letting you know that you don't need to know.
But if you still believe that you need these variables you have two options:
1 write a new class which inherits from this class. That will be able to access the protected variables.
2 edit the library files to move those variables into the public section.
MorganS:
Protected means you don't need to access that from outside the class. The class defines all the ways that you are allowed to access it and private and protected variables are internal implementation details that have no use outside the class. By making it protected the original programner is letting you know that you don't need to know.
But if you still believe that you need these variables you have two options:
1 write a new class which inherits from this class. That will be able to access the protected variables.
2 edit the library files to move those variables into the public section.
Thankyou. When writing the override does it need to be written as a function which returns the information required, or can i somehow override just the protected status?
When writing the override does it need to be written as a function
There is no overriding involved. Re-read MorganS's statement.
You create a new class that derives from the class with protected functions and fields. The derived class then inherits all the protected fields and members, giving it full access to the base class' data.