Static data members should be declared inside class body but defined outside of class (constant static member is an exemption).
class Account {
public:
// interface functions here
void applyint() { amount += amount * interestRate; }
static double rate() { return interestRate; }
static void rate(double); // sets a new rate
private:
std::string owner;
double amount;
static double interestRate;
static double initRate();
};
Define static data members: don't use static keyword.
Outside of class body.
double Account::interestRate = initRate();
Define static member function: don't use static keyword.
void Account::rate(double newRate)
{
interestRate = newRate;
}
本文详细解释了C++中静态成员变量的声明与定义方式,包括如何在类体内声明,以及如何在类体外定义。同时,文章提供了静态成员函数的正确使用方法。
2642

被折叠的 条评论
为什么被折叠?



