V
V
Vadim Deryabkin2017-07-04 09:24:16
C++ / C#
Vadim Deryabkin, 2017-07-04 09:24:16

How to use const object in different cpp files without copying (C++14)?

There is a base abstract class:

class spi_base {
public:
    virtual int tx ( параметры ) const = 0;
    virtual int rx ( параметры ) const = 0;
    virtual ~spi_base() {}
};

It is the base for a class template with a constexpr constructor (to ensure that the body of the constructor does not contain real-time execution instructions), private constants and mutable variables (of course, an object of such a class will go into RAM). All template methods of this class are marked as const.
template < параметры_шаблона >        
class spi_master_hardware_os : public spi_base {
public:
    constexpr spi_master_hardware_os ( void );

    int     reinit                  ( void ) const;
    int     tx                      ( параметры ) const;

private:
    void    on                      ( void ) const;
    void    off                     ( void ) const;

    mutable bool handler_tx_point_inc_cfg_flag = false;
    ...некоторое_количество_mutable_переменных...
    mutable uint32_t number_items = 0;

    const spi_cfg< параметры_шаблона > cfg;
};

In the user code, an object of this class is created as follows (in the .cpp file):
const spi_master_hardware_os< параметры_шаблона > spi1;

The question is the following. How can you declare a template class object const so that the same object is used in different CPP files?
Let me explain. If I declare (in exactly the same way) this object in a .h file and use its methods in two different .cpp (which have include of this .h file), then each CPP file works with its own copy of this object (which I don’t need).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Titov, 2017-07-04
@Vadimatorikda

Here the question is when this object is created.
If no one uses main before the start of main(), and again before the start of main, then a simple extern is often enough .
If not quite so, then start "spudding" the singleton ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question