以下单例类有什么问题? [英] what's wrong with the following singleton class???
问题描述
// Th:
class T
{
public:
static T * instance();
private:
T(){}
~T(){}
static T * smInstance;
};
// T.cpp:
T * T :: instance()
{
if(smInstance == NULL)
smInstance = new T();
返回smInstance;
}
当我尝试编译上面的代码时,有链接器错误:
... / T.cpp:3:未定义引用`T :: smInstance`
我在gentoo linux下使用gcc 3.4.6,谢谢
// T.h:
class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};
// T.cpp:
T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();
return smInstance;
}
when I try to compile the above code, there is linker error:
.../T.cpp:3: undefined reference to `T::smInstance`
I am using gcc 3.4.6 under gentoo linux, thanks
推荐答案
* yi *** **********@gmail.com :
// Th:
class T
{
public:
static T * instance();
private:
T(){}
~T(){}
static T * smInstance;
};
// T.cpp:
T * T: :instance()
{
if(smInstance == NULL)
smInstance = new T();
返回smInstance;
}
当我尝试编译上面的代码时,有链接器错误:
../T.cpp:3:未定义引用`T :: smInstance`
我我在gentoo linux下使用gcc 3.4.6,谢谢
// T.h:
class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};
// T.cpp:
T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();
return smInstance;
}
when I try to compile the above code, there is linker error:
../T.cpp:3: undefined reference to `T::smInstance`
I am using gcc 3.4.6 under gentoo linux, thanks
你已声明但未定义''smInstance''。
它应该在你的[T.cpp]文件中定义。
但是,只需要做
class T
{
private:
T(){}
T(T const& );
~T(){}
public:
static T&实例()
{
T theInstance;
返回实例;
}
};
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
You have declared but not defined ''smInstance''.
It should be defined in your [T.cpp] file.
But instead, just do
class T
{
private:
T() {}
T( T const& );
~T() {}
public:
static T& instance()
{
T theInstance;
return theInstance;
}
};
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* Alf P. Steinbach:
* Alf P. Steinbach:
* yi ************* @ gmail.com :
// Th:
class T
{
public:
static T * instance();
private:
T(){}
~T(){}
静态T * smInstance;
};
// T.cpp:
T * T :: instance()
{
如果(smInstance == NULL)
smInstance = new T();
返回smInstance;
}
当我尝试编译上面的代码时,有链接器错误:
../T.cpp:3:undefined refer来自`T :: smInstance`
我在gentoo linux下使用gcc 3.4.6,谢谢
你已声明但未定义''smInstance''。
但是,只需要做
类T
{
私人:< T(){}
T(T const& T) );
~T(){}
公开:
静态T&实例()
{
T实例;
// T.h:
class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};
// T.cpp:
T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();
return smInstance;
}
when I try to compile the above code, there is linker error:
../T.cpp:3: undefined reference to `T::smInstance`
I am using gcc 3.4.6 under gentoo linux, thanks
You have declared but not defined ''smInstance''.
It should be defined in your [T.cpp] file.
But instead, just do
class T
{
private:
T() {}
T( T const& );
~T() {}
public:
static T& instance()
{
T theInstance;
应该是
static T theInstance;
当然。
返回实例;
}
};
Should be
static T theInstance;
of course.
return theInstance;
}
};
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A :热门发布。
问:usenet和电子邮件中最烦人的事情是什么?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
如果用于多线程环境,会不会有任何问题?
if the class is used in multi-threaded environment, will there be any
problem ????
这篇关于以下单例类有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!