How to make a C++ class that inherits from only one of two base classes at compile time?

Inq

First time post.  This is a general question about how to do something in C++.  To not waste your time, I'll just ask it generalized.  But if your response is something like, "Why do you want to do something so stupid like that?", I'll give the details below.

General Question

I have a base class with common code (SPIBase).  I have two classes that inherit it that have significantly different implementations (SPIMaster, SPISlave).  In each of these two classes, I expose public/protected functionality that is identical.  I want to write a single class that optionally inherits from one of these two classes.  How should I go about doing that?

Details

I am building a library for some microelectronics (ESP8266).  I use the Arduino IDE, and the library is in C++.  As you might be familiar, SPI is a methodology/protocol for communicating between two micro-controllers or a micro-controller and something like a sensor.  A micro-controller is always a Master and the other device/sensor is always a Slave.  The Master completely controls the communication process in SPI.  The Slave is normally relatively passive.   I've written the library to work only between two ESP8266 micro-controllers to form a TCP/IP like protocol where either can send a message to the other un-solicited.  Basically, the Master/Slave issues are hidden and the developer using the library can setup their protocol any way they prefer and in either direction.  (i.e.  Client/Server/Peer messaging, send/response, Streams, etc).  Although, Master/Slave issues are hidden, obviously, one ESP8266 must use one base implementation and the other ESP8266 must use the other.

I now want to write the layer that implements Streams / File transfers.  This class will ride on one or the other of the two intermediate classes, but will be identical except in which class they inherit.  They both will be both senders and receivers.  I'd rather not have two complete classes that are really identical in their implementation.  In this case, I don't really want any portion of one chain of code to be on the other device.  My original thought proved after research not to be possible was to basically... have the library using developer use #define MASTER or #define SLAVE in their main program file and the streaming class would inherit one or the other classes based on this.  I soon discovered that #defines aren't visible downstream.   Is there some C++ design methodology / pattern that will get the results I want?   Thanks for your time.

Miles Budnek

You can use templates. For example:

template <typename SPIImpl>
class SPIStream : public SPIImpl
{
    // stream implementation using the common interface
};

You would then declare an instance of the class like this:

SPIStream<SPIMaster> spi_master_stream;

OR

SPIStream<SPISlave> spi_slave_stream;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to make a constructor of class that inherits from another class in python?

分類Dev

How to get List of classes, inheriting from one class and one interface / or instead from two interfaces

分類Dev

Can a base class be declared in C++ such that classes derived from it can only be created via a creation function?

分類Dev

How to use a generic with two different classes but one of them from the VCL

分類Dev

How to typehint mixins if the target class for the mixin inherits from a metaclass?

分類Dev

Swift 2.0: Parametrized classes don't call proper == function if it inherits from class that is Equatable

分類Dev

How to compile a class type dynamically only once?

分類Dev

How to make a function accessible to certain classes only?

分類Dev

Cast a struct to a class inherits from the same Interface

分類Dev

How can I use std::make_tuple at compile time?

分類Dev

How to make one item selected at a time?

分類Dev

Getting data from a class that inherits from another class

分類Dev

How can you compare two character strings statically at compile time

分類Dev

Semantic-UI: Two Sidebars Conflict, Only One at a Time?

分類Dev

how to modify bootstrap tooltip only for one class

分類Dev

How to set value for a base class from derived class in c# wpf

分類Dev

How do I call a derived class method from the base class constructor in C++?

分類Dev

How to make a member function of a class in C++ generate a different random number each time it is called?

分類Dev

How to make C# chart with two columns per one X value?

分類Dev

How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

分類Dev

c++ How split one line into two from a text file then store each into two different arrays?

分類Dev

How to merge two case classes and store them as one into a table?

分類Dev

How to Deploy Lambdas from one code base?

分類Dev

How to reference one classes variable to another class for manipulation?

分類Dev

Going from Java to C++: how to use one custom class var within another custom class?

分類Dev

How to create compile-time constant in Kotlin from enum?

分類Dev

Class inherits from interface with extra property causes error

分類Dev

Print only one line at a time from text file

分類Dev

Remove only one matching element from two arrays

Related 関連記事

  1. 1

    How to make a constructor of class that inherits from another class in python?

  2. 2

    How to get List of classes, inheriting from one class and one interface / or instead from two interfaces

  3. 3

    Can a base class be declared in C++ such that classes derived from it can only be created via a creation function?

  4. 4

    How to use a generic with two different classes but one of them from the VCL

  5. 5

    How to typehint mixins if the target class for the mixin inherits from a metaclass?

  6. 6

    Swift 2.0: Parametrized classes don't call proper == function if it inherits from class that is Equatable

  7. 7

    How to compile a class type dynamically only once?

  8. 8

    How to make a function accessible to certain classes only?

  9. 9

    Cast a struct to a class inherits from the same Interface

  10. 10

    How can I use std::make_tuple at compile time?

  11. 11

    How to make one item selected at a time?

  12. 12

    Getting data from a class that inherits from another class

  13. 13

    How can you compare two character strings statically at compile time

  14. 14

    Semantic-UI: Two Sidebars Conflict, Only One at a Time?

  15. 15

    how to modify bootstrap tooltip only for one class

  16. 16

    How to set value for a base class from derived class in c# wpf

  17. 17

    How do I call a derived class method from the base class constructor in C++?

  18. 18

    How to make a member function of a class in C++ generate a different random number each time it is called?

  19. 19

    How to make C# chart with two columns per one X value?

  20. 20

    How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

  21. 21

    c++ How split one line into two from a text file then store each into two different arrays?

  22. 22

    How to merge two case classes and store them as one into a table?

  23. 23

    How to Deploy Lambdas from one code base?

  24. 24

    How to reference one classes variable to another class for manipulation?

  25. 25

    Going from Java to C++: how to use one custom class var within another custom class?

  26. 26

    How to create compile-time constant in Kotlin from enum?

  27. 27

    Class inherits from interface with extra property causes error

  28. 28

    Print only one line at a time from text file

  29. 29

    Remove only one matching element from two arrays

ホットタグ

アーカイブ