Я столкнулся с этим фрагментом кода в разделе «Программирование игрового ИИ на примере»:
Код: Выделить всё
/* ------------------ MyClass.h -------------------- */
#ifndef MY_SINGLETON
#define MY_SINGLETON
class MyClass
{
private:
// member data
int m_iNum;
//constructor is private
MyClass(){}
//copy ctor and assignment should be private
MyClass(const MyClass &);
MyClass& operator=(const MyClass &);
public:
//strictly speaking, the destructor of a singleton should be private but some
//compilers have problems with this so I've left them as public in all the
//examples in this book
~MyClass();
//methods
int GetVal()const{return m_iNum;}
static MyClass* Instance();
};
#endif
/* -------------------- MyClass.cpp ------------------- */
//this must reside in the cpp file; otherwise, an instance will be created
//for every file in which the header is included
MyClass* MyClass::Instance()
{
static MyClass instance;
return &instance;
}
Так что же говорит стандарт? Что говорят компиляторы, не соответствующие требованиям? Верно ли утверждение автора, и если да, можете ли вы назвать некоторые компиляторы, которые создавали бы несколько экземпляров, если бы getInstance() был объявлен в заголовках?
Подробнее здесь: https://stackoverflow.com/questions/138 ... ader-files
Мобильная версия