在C++中初始化私有静态数据成员的最佳方法是什么?我在头文件中尝试了这个,但它给了我奇怪的链接器错误:
class foo
{
private:
static int i;
};
int foo::i = 0;
Run Code Online (Sandbox Code Playgroud)
我猜这是因为我无法从课外初始化私人成员.那么最好的方法是什么?
我想为一个类(在这种情况下是一个形状工厂)有一个私有静态常量.
我想要有类似的东西.
class A {
private:
static const string RECTANGLE = "rectangle";
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我从C++(g ++)编译器中得到了各种错误,例如:
ISO C++禁止成员'RECTANGLE'的初始化
非整数类型'std :: string'的静态数据成员的无效类初始化
错误:使'RECTANGLE'静止
这告诉我这种成员设计不符合标准.如何在不使用#define指令的情况下拥有私有文字常量(或者可能是公共的)(我想避免数据全局性的丑陋!)
任何帮助表示赞赏.
对于许多问题,答案似乎可以在"标准"中找到.但是,我们在哪里找到它?最好是在线.
谷歌搜索有时会觉得徒劳,尤其是对于C标准,因为他们在编程论坛的大量讨论中被淹没.
要开始这个,因为这些是我现在正在搜索的,那里有很好的在线资源:
编译以下代码并得到错误type illegal.
int main()
{
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case"sda":
}
}
Run Code Online (Sandbox Code Playgroud)
你不能在任何一个switch或中使用字符串case.为什么?是否有任何解决方案可以很好地支持类似于切换字符串的逻辑?
Clang警告(当使用-Weverything或时Wglobal-constructors)关于静态对象的构造函数.
warning: declaration requires a global constructor
[-Wglobal-constructors]
A A::my_A; // triggers said warning
^~~~
Run Code Online (Sandbox Code Playgroud)
为什么这是相关的,应该如何处理这个警告?
简单示例代码:
class A {
// ...
static A my_A;
A();
};
A A::my_A; // triggers said warning
Run Code Online (Sandbox Code Playgroud) 当我尝试编译时,我收到此错误:
1>------ Build started: Project: snake, Configuration: Debug Win32 ------ 1> exercise.cpp 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(13): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(16): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(19): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(22): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(25): error C2059: syntax error : '>' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(28): error C2059: syntax error : '==' 1>c:\users\robin\documents\visual …
基本上,情况如下:
我有一个类模板(使用一个length类型的模板参数int),并想要引入一个静态数组.这个数组应长度的length和包含的元素1到length.
代码如下所示:
template<int length>
class myClass{
static int array[length];
};
Run Code Online (Sandbox Code Playgroud)
然后我想写一行用于初始化数组
// of course, the line below does not work as intended.
template<int length> int myClass<length>::array[length]={1,2, ..., length};
Run Code Online (Sandbox Code Playgroud)
(如何)可以实现这一目标?
我最近读完了第一卷.Bruce Eckel在C++中的思考,现在转向将这些知识用于实际应用.
我最近使用静态成员函数并尝试使构造函数静态,编译器对此不满意.我检查了书中的原因但找不到任何原因.
有谁能解释为什么?
PS:在看到一些回复之后,我想提一下,由于我知道C#(和Java)允许构造函数被声明为静态,因此产生了混淆.
我在工作中看到了这种代码:
class FooPlugin : IPlugin // IPlugin is a Microsoft CRM component, it has something special about it's execution
{
static FooPlugin()
{
SomeObject.StaticFunction(); // The guy who wrote it said it's meaningful to this question but he can't remember why.
}
}
Run Code Online (Sandbox Code Playgroud)
知道构造函数上的静态修饰符是什么意思,为什么在这种情况下它是必需的?
我正在尝试为我的应用程序做一个日志.我想添加一个属性,所以我知道在哪个类是日志.我已经开始测试,看它是否有效:
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/common.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/syslog_backend.hpp>
enum severity_levels
{
debug,
info,
warning,
error
};
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::syslog_backend > SinkSysLogBackEnd;
typedef boost::log::sources::severity_logger< severity_levels > BoostLogger;
std::ostream& operator<< (std::ostream& strm, severity_levels level)
{
static const char* strings[] =
{
"debug",
"info",
"warning",
"error"
};
if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
strm << strings[level];
else
strm << static_cast< int >(level);
return strm;
}
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_levels)
BOOST_LOG_ATTRIBUTE_KEYWORD(executable, "Executable", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(className, "Class name", …Run Code Online (Sandbox Code Playgroud) 我正在编写这个东西,static const set<char>包含一些不会改变的元素会很有帮助.
class MyClass {
private:
static const set<char> mySet = ??
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?如果您可以从字符串创建它们会很好mySet("ABC"),但是我无法使语法工作.