The premise of the question is how to pass a templated class as a parameter to another function or at least get the same effect:
Below is a stripped-down version of my code
template<uint8_t _bus>
class Com {
public:
uint8_t write() { return _bus; /* does something with template param */}
};
class Instruction
{
public:
int member;
void transmitFrame(char *msg) {
member = Com.write(msg); /* need to pass in Com object somehow */
}
};
main(){
Com<8> myCom;
Instruction myInstruction;
char[] msg = {'h', 'e', 'l', 'l', 'o'};
myInstruction.transmitFrame(msg);
}
The code above will not compile.
My first thought would be to just extend the Com
class however as far as I am aware you can not extend class templates. My second thought would be to have a template inside a template? (so make the Instruction
class a template class with a template of the Com
template) But I am in the weeds on that one and not sure if that is possible.
The Com
object has been instantiated by the time I am trying to use the Instruction
class. So somehow, I need to get the templated Com
object into the Instruction
class so that I can access it.
At that point I am out of ideas...
CodePudding user response:
You appear to be trying to call write
on the Com
class directly, rather than an instance.
You likely want to pass an instance of Com
to transmitFrame
, but to do that you have to pass the template info.
template <uint8_t N>
void transmitFrame(Com<N> &com, char *msg) {
com.write(msg); /* need to pass in Com object somehow */
}
Or if you know the template value will be 8
.
void transmitFrame(Com<8> &com, char *msg) {
com.write(msg); /* need to pass in Com object somehow */
}