Constant Reference and Pointer
#include <iostream>
using namespace std;
void Test(const int& i)//i is const here and hence can't be modified
{
cout<<i<<endl;
}
void TestNew(int& i)
{
cout<<i<<endl;
}
int* ReturnTest()
{
static int i = 10;
return &i;
}
const int* ReturnTest1()
{
static int i=10;
return &i;
}
int main()
{
int a =10;
Test(a); /* You can still pass non-const argument. It's absolutely ok. But, the vice-versa is not possible as shown in TestNew() method. You can't pass const agrument which to a
function which acceps a non-const parameter */
const int ci = 20;
// TestNew(ci); /* This will give error */
const int *pr = ReturnTest(); /* Still OK. vice versa is not ok as shown in ReturnTest1() */
// int* r2 = ReturnTest1(); /* error */
}
Copyright © Open Sky Technology |