C++ 静态成员与运算符重载详解
1. 静态成员的使用
在 C++ 中,静态成员是属于整个类的成员,而不是属于某个特定的对象。下面以一个二手车行的 Car 类为例进行说明。
#ifndef CAR_H
#define CAR_H
#include <iostream>
class Car
{
public:
enum { MAX_NAME_SIZE = 50 };
Car();
Car(const Car &);
Car(int theYear, const char* theMake, const char* theModel);
void print(std::ostream&) const;
const char* make () const { return make_; }
const char* model() const { return model_; }
int year () const { return year_; }
static int numCars() { return numCars_;}
void buy () { ++numCars_; }
void sell () { --numCars_; }
private:
char make_[MAX_NAME_SIZE], model_[MAX_NAME_SIZE];
int year_;
s
超级会员免费看
订阅专栏 解锁全文
1932

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



