Материал из C\C++ эксперт
Returning a pointer from function
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string* ptrToElement(vector<string>* const pVec, int i);
int main()
{
vector<string> i;
i.push_back("A");
i.push_back("B");
i.push_back("C");
cout << *(ptrToElement(&i, 0)) << "\n\n";
string* pStr = ptrToElement(&i, 1);
cout << *pStr << "\n\n";
string str = *(ptrToElement(&i, 2));
cout << str << "\n\n";
*pStr = "test";
cout << i[1] << endl;
return 0;
}
string* ptrToElement(vector<string>* const pVec, int i)
{
return &((*pVec)[i]);
}
Use a function prototype to enforce strong type checking
#include <iostream>
#include <cstdlib>
using namespace std;
void f(int i); // prototype
int main()
{
int x = 10;
f(x);
return 0;
}
void f(int i)
{
i = i * i;
cout << i;
}
100
Use a function"s definition as its prototype, appears before main
#include <iostream>
using namespace std;
bool isEven(int num) {
if(!(num ))
return true;
return false;
}
int main()
{
if(isEven(4))
cout << "4 is even\n";
if(isEven(3))
cout << "this won"t display";
return 0;
}
4 is even