关键词不同

void C++

  1. 当用作函数返回类型时,void 关键字指定该函数不返回值。当用于函数的参数列表时,void 指定该函数不带参数。当在指针的声明中使用时,void 指定指针是通用的

  2. 如果指针的类型为 void *,则指针可以指向未使用 const 或 volatile 关键字声明的任何变量。除非将其转换为其他类型,否则无法取消引用 void 指针。void 指针可以转换为任何其他类型的数据指针。

  3. void 指针可以指向函数,但不能指向 C++中的类成员。

    void vobject;   // C2182  
    void *pv;   // okay  
    int *pint; int i;  
    int main() {  
    pv = &i;  
       // Cast optional in C required in C++  
    pint = (int *)pv;  
    

易失性 C++

  1. 一种类型限定符,可用于声明可以通过硬件在程序中修改对象。

    volatile declarator ;
    

虚拟 C++

  1. virtual 关键字声明虚函数或虚基类。

    virtual [type-specifiers] member-function-declarator  
    virtual [access-specifier] base-class-name 
    

参数

  1. type-specifiers 指定虚拟成员函数的返回类型。

  2. member-function-declarator 声明成员函数。

  3. access-specifier 定义对基类(public,protected 或 private)的访问级别。可以出现在 virtual 关键字之前或之后。

  4. base-class-name 标识先前声明的类类型

这个指针

  1. this 指针是一个只能在类,结构或联合类型的非静态成员函数中访问的指针。它指向调用成员函数的对象。静态成员函数没有 this 指针。

    this->member-identifier  
    

对象的 this 指针不是对象本身的一部分; 它没有反映在对象的 sizeof 语句的结果中。相反,当为对象调用非静态成员函数时,编译器将对象的地址作为函数的隐藏参数传递。例如,以下函数调用:

myDate.setMonth( 3 );  

can be interpreted this way:

setMonth( &myDate, 3 );  

The object's address is available from within the member function as the this pointer. Most uses of this are implicit. It is legal, though unnecessary, to explicitly use this when referring to members of the class. For example:

void Date::setMonth( int mn )  
{  
   month = mn;            // These three statements  
   this->month = mn;      // are equivalent  
   (*this).month = mn;  
}  

The expression *this is commonly used to return the current object from a member function:

return *this;  

The this pointer is also used to guard against self-reference:

if (&Object != this) {  
// do not execute in cases of self-reference 

尝试,抛出和捕获语句(C++)

  1. 要在 C++中实现异常处理,可以使用 try,throw 和 catch 表达式。
  2. 首先,使用 try 块来包含一个或多个可能引发异常的语句。
  3. throw 表达式表示异常条件 - 通常是 try 块中发生的错误。你可以使用任何类型的对象作为 throw 表达式的操作数。通常,此对象用于传递有关错误的信息。在大多数情况下,我们建议你使用 std::exception 类或标准库中定义的派生类之一。如果其中一个不合适,我们建议你从 std::exception 派生自己的异常类。
  4. 要处理可能抛出的异常,请在 try 块之后立即实现一个或多个 catch 块。每个 catch 块指定它可以处理的异常类型。
    MyData md;  
try {  
   // Code that could throw an exception  
   md = GetNetworkResource();  
}  
catch (const networkIOException& e) {  
   // Code that executes when an exception of type  
   // networkIOException is thrown in the try block  
   // ...  
   // Log error message in the exception object  
   cerr << e.what();  
}  
catch (const myDataFormatException& e) {  
   // Code that handles another exception type  
   // ...  
   cerr << e.what();  
}  
  
// The following syntax shows a throw expression  
MyData GetNetworkResource()  
{  
   // ...  
   if (IOSuccess == false)  
      throw networkIOException("Unable to connect");  
   // ...  
   if (readError)  
      throw myDataFormatException("Format error");   
   // ...  
}

try 子句之后的代码是受保护的代码段。throw 表达式抛出 - 即引发异常。catch 子句之后的代码块是异常处理程序。这是捕获抛出异常的处理程序,如果 throw 和 catch 表达式中的类型兼容。

    try {  
   throw CSomeOtherException();  
}  
catch(...) {  
   // Catch all exceptions – dangerous!!!  
   // Respond (perhaps only partially) to the exception, then  
   // re-throw to pass the exception to some other handler  
   // ...  
   throw;  
}

朋友(C++)

  1. 在某些情况下,授予对不是类成员的函数或单独类中的所有成员的成员级访问权限会更方便。只有类实现者可以声明其朋友是谁。函数或类不能将自己声明为任何类的朋友。在类定义中,使用 friend 关键字和非成员函数或其他类的名称来授予它对类的私有成员和受保护成员的访问权限。在模板定义中,可以将类型参数声明为朋友。

  2. 如果声明之前未声明的友元函数,则该函数将导出到封闭的非类作用域。

    class friend F  
    friend F;
    class ForwardDeclared;// Class name is known.  
    class HasFriends  
    {  
       friend int ForwardDeclared::IsAFriend();// C2039 error expected  
    };  
    

朋友的功能

  1. 友元函数是一个函数,它不是类的成员,但可以访问类的私有和受保护成员。友元函数不被视为类成员; 它们是具有特殊访问权限的普通外部函数。

  2. 朋友不在类的范围内,并且不使用成员选择运算符(。和 - >)调用它们,除非它们是另一个类的成员。

  3. 友元函数由授予访问权限的类声明。朋友声明可以放在类声明中的任何位置。它不受访问控制关键字的影响。

    #include <iostream>  
    
    using namespace std;  
    class Point  
    {  
        friend void ChangePrivate( Point & );  
    public:  
        Point( void ) : m_i(0) {}  
        void PrintPrivate( void ){cout << m_i << endl; }  
    
    private:  
    int m_i;  
    };  
    
    void ChangePrivate ( Point &i ) { i.m_i++; }  
    
    int main()  
    {  
       Point sPoint;  
       sPoint.PrintPrivate();  
       ChangePrivate(sPoint);  
       sPoint.PrintPrivate();  
        // Output: 0  
               1  
    }  
    

类成员为朋友

class B;  

class A {  
public:  
   int Func1( B& b );  

private:  
   int Func2( B& b );  
};  

class B {  
private:  
int _b;  

   // A::Func1 is a friend function to class B  
   // so A::Func1 has access to all members of B  
   friend int A::Func1( B& );  
};  

int A::Func1( B& b ) { return b._b; }   // OK  
int A::Func2( B& b ) { return b._b; }   // C2248