C++中的一些概念

摘自C++ primer


C++拷貝構造函數,參數是該類類型本身的引用,通常是const類型的引用

The constructor that takes a single parameter that is a (usually const ) reference to an object of
the class type itself is called the copy constructor

Copy initialization
always involves the copy constructor


When the parameter or return type is a class type, the copy is done by the copy constructor. For
example, consider our make_plural function from page 248 :
// copy constructor used to copy the return value;
// parameters are references, so they aren't copied
string make_plural(size_t, const string&, const string&);
This function implicitly uses the string copy constructor to return the plural version of a given
word. The parameters are const references; they are not copied.
返回值是個string的類類型,因此會調用string的拷貝構造函數
當參數或者返回值是類類型的時候,拷貝是由拷貝構造函數完成的
注意到返回值是類類型的時候,也需要調用拷貝構造函數




概念2:重載操作符 
摘自C++ primer
Overloaded operators are functions that have the name operator followed by the symbol for the
operator being defined


一個賦值操作符的例子

class Sales_item {
public:
// other members as before
// equivalent to the synthesized assignment operator
Sales_item& operator=(const Sales_item &);
};


// equivalent to the synthesized assignment operator
Sales_item&
Sales_item::operator=(const Sales_item &rhs)
{
isbn = rhs.isbn; // calls string::operator=
units_sold = rhs.units_sold; // uses built-in int assignment
revenue = rhs.revenue; // uses built-in double assignment
return *this;
}


Most operators may be defined as member or nonmember functions. When an operator is a member function, its first operand is implicitly bound to the this pointer. Some operators,assignment among them, must be members of the class for which the operator is defined.Usually, the right-hand operand is passed as a const reference


大多數操作符既可以定義成成員操作符,也可以定義成非成員操作符。當一個操作符定義成成員函數的時候,第一個操作數隱式綁定到this指針。某些操作符,比如它們中的賦值操作符,必須是該定義的操作符所在類的成員函數


右操作符通常情況下是const引用。


With the exception of the function-call operator, an overloaded operator has the same number
of parameters (including the implicit this pointer for member functions) as the operator has
operands. The function-call operator takes any number of operands.

// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& Sales_item::operator+=(const Sales_item&);
// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);


When an operator is a member function, this points to the left-hand operand. Thus, the
nonmember operator+ defines two parameters, both references to const Sales_item objects.
Even though compound assignment is a binary operator, the member compound-assignment
operator takes only one (explicit) parameter. When the operator is used, a pointer to the lefthand
operand is automatically bound to this and the right-hand operand is bound to the
function's sole parameter.
It is also worth noting that compound assignment returns a reference and the addition operator
returns a Sales_item object. This difference matches the return types of these operators when
applied to arithmetic types: Addition yields an rvalue and compound assignment returns a
reference to the left-hand operand.

賦值操作符返回的是Sale_item引用類型,+操作符返回的是Sale_item類型



概念3:虛函數virtual function

The purpose of the virtual keyword is to enable dynamic binding. By default, member functions are
nonvirtual. Calls to nonvirtual functions are resolved at compile time. To specify that a function is
virtual, we precede its return type by the keyword virtual .

Any nonstatic member function, other than a constructor, may be virtual.

The virtual keyword appears only on the member-function declaration inside the class. The virtual keyword may not be used on a function definition that appears outside the class body.
任何非靜態的成員函數,除了構造器之外,都可以是虛函數

在函數體之外的函數定義中添加virtual關鍵字都是非法的
比如
virtual void Base::print(){
}
這種就是非法的


Ordinarily, derived classes redefine the virtual functions that they inherit, although they are not requried to do so. If a derived class does not redefine a virtual, then the version it uses is the one defined in its
base class.

派生類繼承基類的虛函數時,virtual關鍵字可加可不加。但爲了代碼可讀性,一般還是需要加的。
下列代碼派生類中未加virtual關鍵字編譯器仍能識別該函數是虛函數。
#include<iostream>
using namespace std;
class Vehicle{
public:
	virtual void run(){
		cout<<"Vehicle run"<<endl;
	}
};
class Car:public Vehicle{
public:
	 void run(){
		cout<<"Car run"<<endl;
	}	
};

int main(){
	Car c;
	Vehicle *v=&c;
	v->run();  //invocate Car's run method
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章