我知道非常量静态变量需要在类定义之外进行初始化,但有没有理由呢?
class A {
static int x = 0 // compile error;
static int y;
};
int A::y = 0; // fine
Run Code Online (Sandbox Code Playgroud) 当静态成员变量在类中声明为private时,如何定义它?
假设我有以下类声明
class static_demo
{
private:
static int a;
public:
static int b;
void set(int x, int y)
{
a = x;
b = y;
}
void show()
{
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";
}
};
Run Code Online (Sandbox Code Playgroud)
然后,要定义的以下语句a将导致编译错误.
int static_demo::a;
Run Code Online (Sandbox Code Playgroud)
那么有可能在类的私有部分中有一个静态数据成员吗?
根据Greg添加完整代码,
#include <iostream>
using namespace std;
class static_demo
{
private:
static int a;
public:
static int b;
void set(int x, int y)
{
a …Run Code Online (Sandbox Code Playgroud) 在C和C++中,所有静态变量默认初始化为ZERO.
这不是静态类数据成员的情况.这是为什么?
#include <iostream>
using namespace std;
int var;
class MyClass
{
public:
static int classVar;
};
int MyClass::classVar = 0; // Why I have to init it here?
int main(void)
{
cout << ::var << endl; // this is initalized to ZERO by default
static int var;
cout << var << endl; // and this also is initalized to Zero
cout << MyClass::classVar << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个静态成员的类.这将使用同一个类的私有静态函数进行初始化.
#include <iostream>
#include <string>
class A
{
public:
static std::string const s;
private:
static std::string make()
{
return "S";
}
};
std::string const A::s = A::make();
int main()
{
std::cout << A::s << std::endl;
// std::cout << A::make() << std::endl; // <-- Does not work
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:由于这个规则是允许的吗?很明显,评论的部分不起作用,因为我不允许从课外访问私人函数.那么为什么在启动期间私有静态成员的初始化是一个特殊情况呢?(并且在旁注:这条规则的目的是什么?是否允许这个确切的情况?)
我知道其他初始化静态成员的机制(如下所示:初始化私有静态成员).但在我的情况下,成员是const,所以据我所知,设置它的唯一方法是通过定义位置的直接初始化.
c++ static-methods initialization static-members language-lawyer
我最近在一些正在开发的大型程序中发现了一个烦人的问题; 我想了解如何以最佳方式解决它.我将代码剪切到以下最小的示例.
#include <iostream>
using std::cin;
using std::cout;
class MagicNumbers
{
public:
static const int BIG = 100;
static const int SMALL = 10;
};
int main()
{
int choice;
cout << "How much stuff do you want?\n";
cin >> choice;
int stuff = (choice < 20) ? MagicNumbers::SMALL : MagicNumbers::BIG; // PROBLEM!
cout << "You got " << stuff << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当使用-O0或-O1进行编译时,我在gcc 4.1.2中遇到链接错误,但在使用-O2或-O3进行编译时一切正常.无论优化选项如何,它都可以使用MS Visual Studio 2005很好地链接.
test.cpp :(.text + 0xab):未定义的引用`MagicNumbers :: SMALL'
test.cpp :(.text + 0xb3):未定义引用`MagicNumbers …
所以,假设我有一个这样的标题:
#ifndef BASECLASS_H
#define BASECLASS_H
class BaseClass
{
public:
static int getX(){return x;}
private:
static int x;
};
int BaseClass::x = 10;
#endif
Run Code Online (Sandbox Code Playgroud)
我多次听说过我不应该在头文件中初始化静态变量,而是在cpp中.但是因为有守卫,所以应该只有一个BaseClass :: x副本.所以我有点不明白为什么要放
int BaseClass::x = 10;
Run Code Online (Sandbox Code Playgroud)
在cpp.谢谢.
我无法使用静态变量和c ++类获得正确的语法.
这是一个简单的例子来显示我的问题.
我有一个函数更新一个应该对所有对象都相同的变量,然后我有另一个想要使用该变量的函数.在这个例子中,我只是返回它.
#include <QDebug>
class Nisse
{
private:
//I would like to put it here:
//static int cnt;
public:
void print()
{
//And not here...!
static int cnt = 0;
cnt ++;
qDebug() << "cnt:" << cnt;
};
int howMany()
{
//So I can return it here.
//return cnt;
}
};
int main(int argc, char *argv[])
{
qDebug() << "Hello";
Nisse n1;
n1.print();
Nisse n2;
n2.print();
}
Run Code Online (Sandbox Code Playgroud)
print函数中的当前本地静态是该函数的本地静态,但我希望它是"类中的私有和全局".
感觉我在这里缺少一些基本的:s c ++语法.
/谢谢
方案:
我错过了
int Nisse::cnt = …Run Code Online (Sandbox Code Playgroud) 我在头文件中有一个类:
class Employee
{
//Private data members
private:
string firstName;
string lastName;
char gender;
//number of employees
const static int numEmployees = 0;
public:
....
};
Run Code Online (Sandbox Code Playgroud)
愚蠢的事情是在教师的"GUIDELINE"中说,在类的私有成员中声明numEmployees为静态整数值0
问题是我无法更新numEmployees变量,因为它是const,例如当您在公共场所声明构造函数时:..您无法增加numEmployees = numEmployees + 1.
如果不申报numEmployees的const,只是做static int numEmployees;的Visual Studio 2010报错说,只有const将在课堂上宣布.
知道如何申报numEmployees吗?谢谢!
可能重复:
初始化私有静态成员
这真的让我发疯,我想在我将用作共享内存的类中声明一个静态私有向量.
我的矢量声明是这样的:
private: static vector< pair< string, bool > > flags;
Run Code Online (Sandbox Code Playgroud)
这是在类中完成的,但是如何将其初始化为空向量?最好的情况是如果init本身就在类中,因为我需要在很多地方使用它.另一种选择就是main()但仅此而已.
我有setFlag()和getFlag()方法使用向量,但它给了我各种链接器错误,因为只有声明,没有定义!
可能的重复:
C++:对静态类成员的未定义引用
记录器.h:
class Logger {
private:
Logger();
static void log(const string& tag, const string& msg, int level);
static Mutex mutex;
public:
static void fatal(const string&, const string&);
static void error(const string&, const string&);
static void warn(const string&, const string&);
static void debug(const string&, const string&);
static void info(const string&, const string&);
};
Run Code Online (Sandbox Code Playgroud)
记录器.cpp:
#include "Logger.h"
#include <sstream>
ofstream Logger::archivoLog;
void Logger::warn(const string& tag, const string& msg){
Logger::mutex.lock();
log(tag, msg, LOG_WARN);
Logger::mutex.unlock();
}
Run Code Online (Sandbox Code Playgroud)
编译时,我收到此错误:
other/Logger.o: In function `Logger::warn(std::basic_string<char, std::char_traits<char>, std::allocator<char> …Run Code Online (Sandbox Code Playgroud)