64,685
社区成员
发帖
与我相关
我的任务
分享
// header file
class Account {
// ...
private:
static const int nameSize = 16;
static const char name[nameSize];
};
// text file 这两句话到底该放哪里?文本文件?
const int Account::nameSize; // member definition needed
const char Account::name[nameSize] = "Savings Account";
//main.cpp
#include "stdafx.h"
#include "Account.h"
int main(int argc, char* argv[])
{
Account a;
return 0;
}
// header file
#define NAME_SIZE 16
class Account {
// ...
private:
static const char name[NAME_SIZE];
};
//类实现文件 *.CPP
const char Account::name[nameSize] = "Savings Account";
class Account {
// ...
private:
static const int nameSize ;
static const char name[];
};
// text file 这两句话到底该放哪里?文本文件?
const int Account::nameSize=16; // member definition needed
const char Account::name[nameSize] = "Savings Account";
int main(int argc, char* argv[])
{
Account a;
return 0;
}
在C++Primer中提到可以在类内对static const变量赋值,但需要在类外定义