Wednesday, March 7, 2012
How to declare/access the pointer variable inside the class in C++?
Declaring the pointer variable inside the class is similar to normal C declaration only. While allocating the memory and access the variable will differ. It will be good to have a memory allocation in constructor and deallocation in destructor. See the sample code below for more clarity.
In the below sample code, there is class ptr with one integer data member i and one integer pointer data member p. These are initialised in constructor using new for pointer variable. In destructor used delete for freeing the dynamic memory. For accessing the pointer data using the instance P and O as saind in main().
*P->p gives the value of the variable and not P->*p. Where as P->p gives the address of the pointer variable. Similarly for object O, *O.p gives the value of the variable and not O.*p. Where as O.p gives the address of the pointer variable.
Output:
P->i is 10
P->p is 0xfda4030
*P->p is 20
O.i is 10
O.p is 0xfda4050
*O.p is 20
In the below sample code, there is class ptr with one integer data member i and one integer pointer data member p. These are initialised in constructor using new for pointer variable. In destructor used delete for freeing the dynamic memory. For accessing the pointer data using the instance P and O as saind in main().
*P->p gives the value of the variable and not P->*p. Where as P->p gives the address of the pointer variable. Similarly for object O, *O.p gives the value of the variable and not O.*p. Where as O.p gives the address of the pointer variable.
using namespace std; class ptr { public: int i; int *p; ptr(); ~ptr(); }; ptr::ptr() // constructor { i=10; p=new int; *p = 20; } ptr::~ptr() //destructor { delete p; } int main() { ptr *P=new ptr(); //dynamic instance cout<<"P->i is "<<P->i<<endl; //accessing normal variable cout<<"P->p is "<<P->p<<endl; // will get address of the poitner variable cout<<"*P->p is "<<*P->p<<endl; // accessing the pointer variable. P->*p is a invalid expression ptr O; //static instance cout<<"O.i is "<<O.i<<endl; //accessing normal int variable cout<<"O.p is "<<O.p<<endl; // will get the address of the int pointer cout<<"*O.p is "<<*O.p<<endl; //accessing the poiner variable. O.*p is a invalid expression }
Output:
P->i is 10
P->p is 0xfda4030
*P->p is 20
O.i is 10
O.p is 0xfda4050
*O.p is 20
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment