讓說我內MyTools.h
如何定義「全局」結構?
#ifndef _MYTOOLS_
#define _MYTOOLS_
typedef struct {
// const
double LN20;
double LN40;
// methods
double NoteToFrequency(int noteNumber);
} Tool;
extern const Tool tool;
#endif // !_MYTOOLS_
decleared這對於每個編譯單元中,只有一個全球性的/常量/獨特的Tool
實例。正是我想要的。
但現在:我該如何定義它?在.h
我只宣佈它。我如何在.cpp
中定義它?試過一些像這樣的:
tool.LN20 = 1.34;
但當然它不起作用。該方法的定義?
related/dupe:http://stackoverflow.com/questions/20716091/global-const-object-shared-between-compilation-units – NathanOliver
'const Tool tool {1.34,3.14};'?工作示例:[這裏](https://ideone.com/xiiJ2e)。 –
順便說一下,那些標記爲「// const」的成員實際上並不是*常量。 –