Const completion of the use of heavy-duty

Author:Anonymous    Updated:2008-10-17 12:13:32

(A) completion of the heavy use of the parameters const

 

 

 

 void print (int & t); / / a function

 

 void print (const int & t); / / Function II

 

 int i = 3;

 

 const int j = 4;

 

 print (i); / / one sentence: Call a function

 

 print (j); / / Second sentence: the second call function

 

 

 

 If there is no one function, more than one sentence statement and the second function can be called the second. If you have a function, then a statement will call a function. Second, if there is no such function, the second statement compile error can not be a const object to the transfer of non-const reference.

 

 The value of the transfer, as is the Senate-passed into the function of reproduction, it can not change the function within parameters. In that case, not increase it for const reference is no difference. Only passing reference and guide, const is the real protection is the Senate. Function call option is based on the actual conduct of the Senate, so that only passing reference guide and can be used whether or not to increase const overloaded.

 

 

 

 (B) the use of heavy load complete const member functions

 

 

 

 class A (

 

 public:

 

 int f () (return 3;) / / a function

 

 int f () const (return 4;) / / function II

 

 );

 

 

 

 int main ()

 

 (

 

 A a1;

 

 const A a2;

 

 

 

 cout <<a1.f () <<endl; / / call a function

 

 cout <<a2.f () <<endl; / / call function II

 

 )

 

 

 

 For a class of the members of the function, you can use it as a const member functions for heavy-duty. Call, if the members of the non-const, the non-priority call const member functions version, if there is, the version of the call const member functions. const member of the only version of the call const member functions, if there are two functions, a2.f () compile error.

Previous:vc + + to limit the size of the window method
Next:c + + to achieve the methods and code calculators
User Reviews
Site Search
Recommended article
AD