CPP Advance Pointers
Methods or functions using advance pointers as arguments
- When you pass a pointer to a function or method the object the pointer points to can be changed or accessed from inside the function or method if no restrictions are involved.
- You are also able to change the pointer address from inside the function or method but as the pointer is a copy of the original this type of change are only local for the function or method.
- To change a pointer you must pass a pointer to pointer of an object(**pAge) as argument to a function or method.
-
A pointer can point to a pointer which can point to a pointer and so on.
Example of using pointer as argument to a method:
#include <iostream> class Person { public: // This Constructor take arguments of // pointer to pointer to integer Person (int ** pAge, int ** pWeight){ // Set the age and weight before // incrementing the references value this->age = (**pAge)++; this->weight = (**pWeight)++; // Swap the address of the pointer to integer // We can do this because we have pointer to pointer to integer arguments int * pInt=*pAge; *pAge=*pWeight; *pWeight=pInt; } // This Constructor take arguments of // pointer to integer Person (int * pAge, int * pWeight){ this->age = (*pAge)++; this->weight = (*pWeight)++; // We can change the pAge pointer and the pWeight pointer // but is would be local in the this contructor. } ~Person() {} int getAge() { return age; } int getWeight() { return weight; } private: int age; int weight; }; int main() { // create two pointer to pointer to integer int ** ppAge = new int*(new int(40)); int ** ppWeight = new int*(new int(60)); // Using Constructor that take arguments of // pointer to pointer to integer Person * pRicard = new Person(ppAge, ppWeight); std::cout << "Ricard is: "; std::cout << pRicard->getAge() << " years old.\n" ; std::cout << "And Ricard weighs: "; std::cout << pRicard->getWeight() << " kilo.\n\n" ; // Using Constructor that take arguments of // pointer to integer Person * pObama = new Person(*ppAge, *ppWeight); std::cout << "Obama is: "; std::cout << pObama->getAge() << " years old.\n" ; std::cout << "And Obama weighs: "; std::cout << pObama->getWeight() << " kilo.\n\n" ; return 0; }
When we run this application the result will be:Ricard is: 40 years old. And Ricard weighs: 60 kilo. Obama is: 61 years old. And Obama weighs: 41 kilo.
You can download this example here (needed tools can be found in the right menu on this page).
Using const specifier with the Method's arguments.
-
To change the first Constructor in the example above with a const specifies
that do not allow any change of the pointer to pointer to integers:
Person (int ** const pAge, int ** const pWeight)
-
If we in the same Constructor do not want to give any possibility to change
the pointer to integer either then the Constructor must be as:
// This Constructor take arguments of // const pointer to const pointer to integer Person ( int * const * const pAge, int * const * const pWeight){ // Set the age and weight before // incrementing the references value this->age = (**pAge)++; this->weight = (**pWeight)++; // Swap the address of the references int * pInt=*pAge; // We can only read pointer to integer now // *pAge=*pWeight; // can not changed now // *pWeight=pInt; // can not changed now }
-
If we in the same Constructor do not want to give any possibility to change
the integer either then the Constructor must be as:
// This Constructor take arguments of // const pointer to const pointer to const integer Person ( const int * const * const pAge, const int * const * const pWeight){ // Set the age and weight before // incrementing the references value this->age = (**pAge); // can not changed of the original integer now this->weight = (**pWeight); // can not changed of the original integer now // Swap the address of the references int * pInt=*pAge; // We can only read pointer to integer now // *pAge=*pWeight; // can not changed now // *pWeight=pInt; // can not changed now }
-
If the argument to a method is of const pointer to const object of a class
and you want to access any method in that object, the methods
must be const specified :
Example of a const pointer to const object using methods of the object:
#include <iostream> class Person { public: // This Constructor take arguments of // pointer to integer Person (int * const pAge, int * const pWeight){ // Set the age and weight this->age = *pAge; this->weight = *pWeight; } // This Constructor take arguments of // const pointer to const Person object Person (const Person * const pPerson){ this->age = pPerson->getAge()+1; this->weight = pPerson->getWeight()+10; } ~Person() {} // as we are using these methods in the last // Constructor they must be supplied with the const specifier int getAge() const { return age; } int getWeight() const { return weight; } private: int age; int weight; }; int main() { int age = 40; int weight = 60; // Using Constructor that take arguments of // pointer to integer Person * pRicard = new Person(&age, &weight); std::cout << "Ricard is: "; std::cout << pRicard->getAge() << " years old.\n" ; std::cout << "And Ricard weighs: "; std::cout << pRicard->getWeight() << " kilo.\n\n" ; // Using Constructor that take arguments of // pointer to Person object Person * pObama = new Person(pRicard); std::cout << "Obama is: "; std::cout << pObama->getAge() << " years old.\n" ; std::cout << "And Obama weighs: "; std::cout << pObama->getWeight() << " kilo.\n\n" ; return 0; }
When we run this application, the result will be:Ricard is: 40 years old. And Ricard weighs: 60 kilo. Obama is: 41 years old. And Obama weighs: 70 kilo.
You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.