Reputation: 43
I am trying to manipulate 'registerTable' with 'ChargerClass::SystemStruct::initNested()', with a function from an object passed in during initialisation 'interface'.
The reason I want to nest is to enable the use of dot notation to use the Class members; there will be a lot of functions in this class and dot notation will make clear what part of the class is being utilised.
'chargerHAL_r hal' must be passed in as it is an abstraction layer object to allow for multiple communication protocols to be used with the ChargerClass.
If initNested() is within 'SystemStruct', I get compiler errors:
However, if initNOTNested() is in the parent class ChargerClass and not nested in SystemStruct. it compiles just fine.
I have included comments in the code snippet for both initNested() and initNOTNested();
I am using C++11, and since this version, nested-class access to enclosing-class members is valid, no?
What am I missing?
struct chargerHAL_r {
static void readAllRegisters(uint8_t *regTable);
};
class ChargerClass {
public:
ChargerClass(chargerConfig_r config, chargerHAL_r hal) {
interface = hal;
}
struct system_r {
bool initNested(); /* Compiler throws error */
};
bool initNotNested(); /* Compiles OK */
private:
uint8_t registerTable[20];
chargerHAL_r interface;
};
/* Compiler throws error */
bool ChargerClass::system_r::initNested() {
interface.readAllRegisters(registerTable);
}
/* Compiles OK */
bool ChargerClass::initNotNested() {
interface.readAllRegisters(registerTable);
}
Upvotes: 2
Views: 3123
Reputation: 281
You may have misunderstood what a nested class is.
A nested class is an independent class with special access privileges. That means defining an instance of system_r
will not be related anyway to ChargerClass
, hence it won't know what interface
or registerTable
you're talking about.
I would consider this as a workaround for your problem.
Upvotes: 1