Stupid C++ member variable declaration

I’m writing a transceiver module at the HAL level. It appears that convention is to have a _driver.cpp file that contains the implementation. And that driver is implemented in a struct… So, I’ve declared things like this:


struct CC1101_Driver
{
     static void  ResetChip(void);
     static UINT8 ReadStatusReg(UINT8 addr);
     ...
}

However, I can’t seem to figure out a good way to declare member variables. You can’t declare them like this:


struct CC1101_Driver
{
     ...
     static BOOL IsTransmitting;
}

because static member variables aren’t allowed in C++. If you remove the “static” keyword, it doesn’t make any sense (since you’d need to instantiate an instance of the struct to access the variable).

What’s the right way to do this? Using namespaces instead? Is that supported by the toolchain?

A C++ struct is very much like a C++ class. A struct can have methods.

To add a variable:

struct CC1101_Driver
{
     static void  ResetChip(void);
     static UINT8 ReadStatusReg(UINT8 addr);
     BOOL IsTransmitting;
}

I am not sure what the static keyword does with the methods.

Actually, it makes perfect sense. A class member variable for a struct does not make sense. structs are mean to be instantiated.

I suggest to spend half an hour and read a tutorial on C++ classes and structs and what is different about them.

Half hour for someone who knows C++. :slight_smile:

Ok, an hour!

I thought I was familiar enough with C/C++ classes, but maybe that’s not the case.

I was under the impression that if I made a struct like this

struct CC1101_Driver
{
     static void  ResetChip(void);
     static UINT8 ReadStatusReg(UINT8 addr);
     BOOL IsTransmitting;
}

I would have to create an instance of it to access the IsTransmitting variable. i.e.:

void CC1101_Driver::ResetChip()
{
CC1101_Driver myDriver;
myDriver.IsTransmitting = false;
}

You’re saying this is not the case?

MDK complains when I try to access those variables in the struct without creating an instance of the class, which would seem to support my interpretation.

I wrapped everything in a namespace and tossed in a few “extern” keywords for good measure, and it seems to be behaving well enough.

You can have static members in a struct but you have to initialize them. Check porting kit code it has plenty of examples.