|
-
June 16th, 2009, 08:46 AM
#1
[RESOLVED] read only alias to a pointer
Hi,
I was wondering if there is a way to have a read only alias to a pointer.
let me explain what I am looking for.
suppose v1 is an int variable, and ptr1 is a pointer to v1.
suppose the alias to ptr1 is ptr2.
I want an alias to ptr1, in such a way that the alias doesn't modify the variable v1.
When ptr1 points to v2, the alias also must point to v2, but should not be able to modify v2.
Is this possible ?
I tried doing it, but the alias that I thought I created, isn't alias to ptr1 in the first place.
Code:
#include <iostream>
using namespace std;
int main()
{
system("clear");
int v1 = 10, v2 = 20;
cout << "&v1 = " << &v1 << "\t\tv1 = " << v1 << endl;
cout << "&v2 = " << &v2 << "\t\tv2 = " << v2 << endl;
cout << "\n\n------------\n\n";
int* ptr1 = &v1;
const int* const & ptr2 = ptr1;
cout << "&ptr1 = " << &ptr1 << "\t\tptr1 = " << ptr1 << "\t*ptr1 = " << *ptr1 << endl;
cout << "&ptr2 = " << &ptr2 << "\t\tptr2 = " << ptr2 << "\t*ptr2 = " << *ptr2 << endl;
cout << "\n\n------------\n\n";
ptr1 = &v2;
cout << "&ptr1 = " << &ptr1 << "\t\tptr1 = " << ptr1 << "\t*ptr1 = " << *ptr1 << endl;
cout << "&ptr2 = " << &ptr2 << "\t\tptr2 = " << ptr2 << "\t*ptr2 = " << *ptr2 << endl;
return(0);
}
Thanks,
Muthu
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|