引用

引用是别名。定义的语法:

int& rSomeRef = someInt;

用指针和引用分别实现交换变量的函数:

#include <iostream>
using namespace std;

void swap(int* x, int* y);

int main(int argc, char *argv[])
{
    
int x = 5, y = 10;

    cout 
<< "Main.Before swap, x: " << x << ", y: " << y << endl;
    swap(
&x, &y);
    cout 
<< "Main.After swap, x: " << x << ", y: " << y << endl;
     
    
return 0;
}


void swap(int* x, int* y) {
    
int temp;

    temp 
= *x;
    
*= *y;
    
*= temp;
}


#include <iostream>
using namespace std;

void swap(int* x, int* y);

int main(int argc, char *argv[])
{
    
int x = 5, y = 10;

    cout 
<< "Main.Before swap, x: " << x << ", y: " << y << endl;
    swap(x, y);
    cout 
<< "Main.After swap, x: " << x << ", y: " << y << endl;
     
    
return 0;
}


void swap(int& x, int& y) {
    
int temp;

    temp 
= x;
    x 
= y;
    y 
= temp;
}


传递 const 指针

下面的例子可以防止程序对 SimpleCat 调用任何非 const 方法,从而保护对象不被改变。

#include <iostream>
using namespace std;

class SimpleCat
{
public:
    SimpleCat();
    SimpleCat(SimpleCat
&);
    
~SimpleCat();

    
int GetAge() const {return itsAge;}
    
void SetAge(int age) {itsAge = age;}

private:
    
int itsAge;
}
;

SimpleCat::SimpleCat() 
{
    cout 
<< "SimpleCat Constructor\n";
    itsAge 
= 1;
}


SimpleCat::SimpleCat(SimpleCat
&{
    cout 
<< "SimpleCat Copy Constructor\n";
}


SimpleCat::
~SimpleCat() {
    cout 
<< "SimpleCat Destructor\n";
}


// 全局函数
const SimpleCat* const FunctionTwo(const SimpleCat* const theCat);

int main(int argc, char *argv[])
{
    cout 
<< "Making a cat\n";
    SimpleCat Frisky;
    cout 
<< "Frisky is " << Frisky.GetAge() << " years old\n";
    
int age = 5;
    Frisky.SetAge(age);
    cout 
<< "Frisky is " << Frisky.GetAge() << " years old\n";
    cout 
<< "Calling FunctionTwo\n";
    FunctionTwo(
&Frisky);
    cout 
<< "Frisky is " << Frisky.GetAge() << " years old\n";
     
    
return 0;
}


const SimpleCat* const FunctionTwo(const SimpleCat* const theCat) {
    cout 
<< "Function Two.Returning\n";
    cout 
<< "Frisky is now " << theCat->GetAge() << " years old\n";
    
return theCat;
}


传递引用作为变通的方法:

#include <iostream>
using namespace std;

class SimpleCat
{
public:
    SimpleCat();
    SimpleCat(SimpleCat
&);
    
~SimpleCat();

    
int GetAge() const {return itsAge;}
    
void SetAge(int age) {itsAge = age;}

private:
    
int itsAge;
}
;

SimpleCat::SimpleCat() 
{
    cout 
<< "SimpleCat Constructor\n";
    itsAge 
= 1;
}


SimpleCat::SimpleCat(SimpleCat
&{
    cout 
<< "SimpleCat Copy Constructor\n";
}


SimpleCat::
~SimpleCat() {
    cout 
<< "SimpleCat Destructor\n";
}


// 全局函数
const SimpleCat& FunctionTwo(const SimpleCat& theCat);

int main(int argc, char *argv[])
{
    cout 
<< "Making a cat\n";
    SimpleCat Frisky;
    cout 
<< "Frisky is " << Frisky.GetAge() << " years old\n";
    
int age = 5;
    Frisky.SetAge(age);
    cout 
<< "Frisky is " << Frisky.GetAge() << " years old\n";
    cout 
<< "Calling FunctionTwo\n";
    FunctionTwo(Frisky);
    cout 
<< "Frisky is " << Frisky.GetAge() << " years old\n";
     
    
return 0;
}


const SimpleCat& FunctionTwo(const SimpleCat& theCat) {
    cout 
<< "Function Two.Returning\n";
    cout 
<< "Frisky is now " << theCat.GetAge() << " years old\n";
    
// theCat.SetAge(8);  // 错误
    return theCat;
}


C++ 中程序没法给引用本身重新赋值,使他指向另一个变量。因此引用总是常数的。如果对引用加上关键词 const, 那么其作用就是使目标对象成为常数对象。

posted on 2005-04-26 00:29  NeilChen  阅读(556)  评论(0编辑  收藏  举报

导航