我想用不同的設置製作多個結構...!他們都擁有相同的變量(相同的名稱等)!但不同的實現!這個設置應該可以從另一個班級到達......這是最好的方式嗎?還是我在做一些完全錯誤的事情?C++多重實例化結構,可以從其他類中調用
A.H:
class A
{
};
struct Color{
unsigned char r;
unsigned char g;
unsigned char b;
};
extern struct Settings settings;
A.cpp
struct settings{
Color firstcolor = {0,0,0}; //error: data member initializer is not allowed
Color secondcolor = {255,255,255}; //error: data member initializer is not allowed
};
struct anothersettings{
Color firstcolor = {255,255,255}; //error: data member initializer is not allowed
Color secondcolor = {0,0,0}; //error: data member initializer is not allowed
};
B.cpp
#include "A.h"
dosomethingwith(settings);
這對我來說很好。我認爲這是http://en.wikipedia.org/wiki/Composite_pattern – 2013-04-08 14:25:06