使用函数返回的值初始化 c++ 标头中的 const 值

Initializing const value in c++ header with value returned by function

本文关键字:const c++ 初始化 返回 函数      更新时间:2023-10-16

有没有办法在 c++ 标头 "foo.hpp" 中定义 conastant foo"

const int foo;

并使用"bar.hpp"中定义的函数栏返回的值对其进行初始化

int bar();

?(无论是在foo.hpp中还是在foo.cpp中。

写入

extern const int foo;

foo.hpp

const int foo = bar();

福.cpp

当然有:

// foo.hpp
const int foo = bar();

正如你在这里看到的,它工作得很好。