C++/Data Type/reference
Содержание
- 1 constant references
- 2 Creating and Using References for integer
- 3 Data Slicing With Passing by Value
- 4 Passing by Reference Using Pointers
- 5 Passing Objects by Reference
- 6 Passing References to Objects
- 7 Reassigning a reference
- 8 References for int type variable
- 9 returning a reference to a string
- 10 Returning multiple values from a function using references
- 11 swap() Rewritten with References
- 12 Taking the Address of a Reference
- 13 using references for int
constant references
<source lang="cpp">
- include <iostream>
- include <string>
- include <vector>
using namespace std; //a constant reference to a vector of strings void display(const vector<string>& v); int main() {
vector<string> v; v.push_back("A"); v.push_back("B"); v.push_back("C"); display(v); return 0;
} void display(const vector<string>& vec) {
for (vector<string>::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) cout << *iter << endl;
}
</source>
Creating and Using References for integer
<source lang="cpp">
- include <iostream>
int main() {
using namespace std; int intOne; int &rSomeRef = intOne; intOne = 5; cout << "intOne: " << intOne << endl; cout << "rSomeRef: " << rSomeRef << endl; rSomeRef = 7; cout << "intOne: " << intOne << endl; cout << "rSomeRef: " << rSomeRef << endl; return 0;
}
</source>
Data Slicing With Passing by Value
<source lang="cpp">
- include <iostream>
using namespace std; class Mammal{
public: Mammal():itsAge(1) { } virtual ~Mammal() { } virtual void Speak() const { cout << "Mammal speak!" << endl; } protected: int itsAge;
}; class Dog : public Mammal{
public: void Speak()const { cout << "Woof!" << endl; }
}; class Cat : public Mammal {
public: void Speak()const { cout << "Meow!" << endl; }
}; void ValueFunction (Mammal); void PtrFunction (Mammal*); void RefFunction (Mammal&); int main() {
Mammal* ptr=0; int choice; while (1) { bool fQuit = false; cout << "(1)dog (2)cat (0)Quit: "; cin >> choice; switch (choice) { case 0: fQuit = true; break; case 1: ptr = new Dog; break; case 2: ptr = new Cat; break; default: ptr = new Mammal; break; } if (fQuit == true) break; PtrFunction(ptr); RefFunction(*ptr); ValueFunction(*ptr); } return 0;
} void ValueFunction (Mammal MammalValue) {
MammalValue.Speak();
} void PtrFunction (Mammal * pMammal) {
pMammal->Speak();
} void RefFunction (Mammal & rMammal) {
rMammal.Speak();
}
</source>
Passing by Reference Using Pointers
<source lang="cpp">
- include <iostream>
using namespace std; void swap(int *x, int *y); int main(){
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 *px, int *py) {
int temp; cout << "Swap. Before swap, *px: " << *px << " *py: " << *py << endl; temp = *px; *px = *py; *py = temp; cout << "Swap. After swap, *px: " << *px << " *py: " << *py << endl;
}
</source>
Passing Objects by Reference
<source lang="cpp">
- include <iostream>
using namespace std; class SimpleCat {
public: SimpleCat (); SimpleCat(SimpleCat&); ~SimpleCat();
}; SimpleCat::SimpleCat() {
cout << "Simple Cat Constructor..." << endl;
} SimpleCat::SimpleCat(SimpleCat&) {
cout << "Simple Cat Copy Constructor..." << endl;
} SimpleCat::~SimpleCat() {
cout << "Simple Cat Destructor..." << endl;
} SimpleCat FunctionOne (SimpleCat theCat); SimpleCat* FunctionTwo (SimpleCat *theCat); int main() {
cout << "Making a cat..." << endl; SimpleCat Frisky; cout << "Calling FunctionOne..." << endl; FunctionOne(Frisky); cout << "Calling FunctionTwo..." << endl; FunctionTwo(&Frisky); return 0;
} // FunctionOne, passes by value SimpleCat FunctionOne(SimpleCat theCat) {
cout << "Function One. Returning... " << endl; return theCat;
} // functionTwo, passes by reference SimpleCat* FunctionTwo (SimpleCat *theCat) {
cout << "Function Two. Returning... " << endl; return theCat;
}
</source>
Passing References to Objects
<source lang="cpp">
- 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 << "Simple Cat Constructor..." << endl; itsAge = 1;
} SimpleCat::SimpleCat(SimpleCat&) {
cout << "Simple Cat Copy Constructor..." << endl;
} SimpleCat::~SimpleCat() {
cout << "Simple Cat Destructor..." << endl;
} const SimpleCat & FunctionTwo (const SimpleCat & theCat); int main() {
cout << "Making a cat..." << endl; SimpleCat Frisky; cout << "Frisky is " << Frisky.GetAge() << " years old" << endl; int age = 5; Frisky.SetAge(age); cout << "Frisky is " << Frisky.GetAge() << " years old" << endl; cout << "Calling FunctionTwo..." << endl; FunctionTwo(Frisky); cout << "Frisky is " << Frisky.GetAge() << " years old" << endl; return 0;
} // functionTwo, passes a ref to a const object const SimpleCat & FunctionTwo (const SimpleCat & theCat) {
cout << "Frisky is now " << theCat.GetAge(); return theCat;
}
</source>
Reassigning a reference
<source lang="cpp">
- include <iostream>
int main() {
using namespace std; int intOne; int &rSomeRef = intOne; intOne = 5; cout << "intOne: " << intOne << endl; cout << "rSomeRef: " << rSomeRef << endl; cout << "&intOne: " << &intOne << endl; cout << "&rSomeRef: " << &rSomeRef << endl; int intTwo = 8; rSomeRef = intTwo; // not what you think! cout << "\nintOne: " << intOne << endl; cout << "intTwo: " << intTwo << endl; cout << "rSomeRef: " << rSomeRef << endl; cout << "&intOne: " << &intOne << endl; cout << "&intTwo: " << &intTwo << endl; cout << "&rSomeRef: " << &rSomeRef << endl; return 0;
}
</source>
References for int type variable
<source lang="cpp">
- include <iostream>
using namespace std; void swap(int *a, int *b);
main(void) {
int x, y; x = 99; y = 88; cout << x << " " << y << "\n"; swap(&x, &y); cout << x << " " << y << "\n"; return 0;
}
void swap(int *a, int *b) {
int t; t = *a; *a = *b; *b = t;
}
</source>
returning a reference to a string
<source lang="cpp">
- include <iostream>
- include <string>
- include <vector>
using namespace std; //returns a reference to a string string& refToElement(vector<string>& v, int i); int main() {
vector<string> v; v.push_back("A"); v.push_back("B"); v.push_back("C"); //displays string that the returned reference refers to cout << refToElement(v, 0) << "\n\n"; //assigns one reference to another -- inexpensive assignment string& rStr = refToElement(v, 1); cout << rStr << "\n\n"; //copies a string object -- expensive assignment string str = refToElement(v, 2); cout << str << "\n\n"; //altering the string object through a returned reference rStr = "Healing Potion"; cout << v[1] << endl; return 0;
} string& refToElement(vector<string>& vec, int i){
return vec[i];
}
</source>
Returning multiple values from a function using references
<source lang="cpp">
- include <iostream>
using namespace std; enum ERR_CODE { SUCCESS, ERROR }; ERR_CODE Factor(int, int&, int&); int main() {
int number, squared, cubed; ERR_CODE result; cout << "Enter a number (0 - 20): "; cin >> number; result = Factor(number, squared, cubed); if (result == SUCCESS) { cout << "number: " << number << endl; cout << "square: " << squared << endl; cout << "cubed: " << cubed << endl; } else cout << "Error encountered!!" << endl; return 0;
} ERR_CODE Factor(int n, int &rSquared, int &rCubed) {
if (n > 20) return ERROR; // simple error code else { rSquared = n*n; rCubed = n*n*n; return SUCCESS; }
}
</source>
swap() Rewritten with References
<source lang="cpp">
- include <iostream>
using namespace std; void swap(int &x, int &y); int main(){
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 &rx, int &ry){
int temp; cout << "Swap. Before swap, rx: " << rx << " ry: " << ry << endl; temp = rx; rx = ry; ry = temp;
cout << "Swap. After swap, rx: " << rx << " ry: " << ry << endl;
}
</source>
Taking the Address of a Reference
<source lang="cpp">
- include <iostream>
int main() {
using namespace std; int intOne; int &rSomeRef = intOne; intOne = 5; cout << "intOne: " << intOne << endl; cout << "rSomeRef: " << rSomeRef << endl; cout << "&intOne: " << &intOne << endl; cout << "&rSomeRef: " << &rSomeRef << endl; return 0;
}
</source>
using references for int
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
int myScore = 1000; int& mikesScore = myScore; // create a reference cout << "myScore is: " << myScore << endl; cout << "mikesScore is: " << mikesScore << endl; myScore += 500; cout << "myScore is: " << myScore << endl; cout << "mikesScore is: " << mikesScore << endl; mikesScore += 500; cout << "myScore is: " << myScore << endl; cout << "mikesScore is: " << mikesScore << endl; return 0;
}
</source>