---
title: "类class（一） "
author: "clskkk2222"
date: "2008-02-14T23:30:29Z"
tags: []
categories: ["编程语言"]
slug: "class"
draft: false
lastmod: "2008-02-14T23:30:29Z"
id: 162363
url: "https://stonelet.iteye.com/blog/162363"
---

最简单地说，类就是定义了一个新的类型和一个新作用域

每个类可以没有成员，也可以定义多个成员，成员可以是数据、函数或类型别名

一个类可以包含若干公有的、私有的和受保护的部分

创建一个类类型的对象时，编译器会自动使用一个构造函数来初始化该对象

构造函数一般就使用一个构造函数初始化列表来初始化对象的数据成员

在类内部，声明成员函数是必需的，而定义成员函数则是可选的，在类内部定义的函数默认为 inline

在类外部定义的成员函数必须指明它们是在类的作用域中

成员函数有一个附加的隐含实参，将函数绑定到调用函数的对象

将关键字 const 加在形参表之后，就可以将成员函数声明为常量

const 成员不能改变其所操作的对象的数据成员，const 必须同时出现在声明和定义中，若只出现在其中

一处，就会出现一个编译时错误

在 C++ 中，使用访问标号来定义类的抽象接口和实施封装

程序的所有部分都可以访问带有 public 标号的成员。类型的数据抽象视图由其 public 成员定义  
使用类的代码不可以访问带有 private 标号的成员。private 封装了类型的实现细节

可以在任意的访问标号出现之前定义类成员，如果类是用 struct 关键字定义的，则在第一个访问标号之

前的成员是公有的；如果类是用 class 关键字是定义的，则这些成员是私有的

除了定义数据和函数成员之外，类还可以定义自己的局部类型名字

类所定义的类型名遵循任何其他成员的标准访问控制

成员函数可被重载，只能重载本类的其他成员函数

在类内部定义的成员函数，将自动作为 inline 处理

可以显式地将成员函数声明为 inline

可以在类定义体内部指定一个成员为inline，作为其声明的一部分，也可以在类定义外部的函数定义上指

定 inline，在声明和定义处指定 inline 都是合法的

像其他 inline 一样，inline 成员函数的定义必须在调用该函数的每个源文件中是可见的，不在类定义

体内定义的 inline 成员函数，其定义通常应放在有类定义的同一头文件中

在一个给定的源文件中，一个类只能被定义一次，如果在多个文件中定义一个类，那么每个文件中的定义

必须是完全相同的

可以声明一个类而不定义它：  
class Screen; // declaration of the Screen class  
这个声明，有时称为前向声明(forward declaraton)，类的前向声明一般用来编写相互依赖的类

不完全类型(incomplete type)只能以有限方式使用：不能定义该类型的对象，不完全类型只能用于定义

指向该类型的指针及引用，或者用于声明(而不是定义)使用该类型作为形参类型或返回类型的函数

在创建类的对象之前，必须完整地定义该类

类不能具有自身类型的数据成员

类的数据成员可以是指向自身类型的指针或引用

只有当类定义已经在前面出现过，数据成员才能被指定为该类类型，如果该类型是不完全类型，那么数据

成员只能是指向该类类型的指针或引用

定义对象时，将为其分配存储空间，但(一般而言)定义类型时不进行存储分配

定义了一个类类型之后，可以按以下两种方式使用：  
1.将类的名字直接用作类型名  
2.指定关键字 class 或 struct，后面跟着类的名字

类的定义分号结束，分号是必需的，因为在类定义之后可以接一个对象定义列表

通常，将对象定义成类定义的一部分是个坏主意

```
#include <string>
#include <iostream>
using namespace std;

class Screen {
public:
     typedef string::size_type index;
     // implicitly inline when defined inside the class declaration
     char get() const { return contents[cursor]; }
     // explicitly declared as inline; will be defined outside the class declaration
     inline char get(index ht, index wd) const;
     // inline not specified in class declaration, but can be defined inline later
     index get_cursor() const;
     // constructor with height and width
     Screen():height(0),width(0){}
     Screen(index h,index w):height(h),width(w) {
          cout <<  height << width << endl;         
     }
private:
    string contents;
    index cursor;
    index height, width;
};
// inline declared in the class declaration; no need to repeat on the definition
char Screen::get(index r, index c) const
{
    index row = r * width;    // compute the row location
    return contents[row + c]; // offset by c to fetch specified character
}
// not declared as inline in the class declaration, but ok to make inline in definition
inline Screen::index Screen::get_cursor() const
{
    return cursor;
}

class LinkScreen {
    Screen window;
    LinkScreen *next;
    LinkScreen *prev;
};

class Y;  //forward declaraton 
class X{
      //Y y      error!
      Y* y;      
};

class Y{
  X x;    
};

int main()
{
    Screen s1;
    class Screen s2;  // inherited from C and is also valid in C++
    struct Screen s3; // inherited from C and is also valid in C++
    
    return 0;
}
```