7.1. Pointer of an object
An object or an instance created from a class can have a pointer as similar to other datatypes
7.1.1 declaration and initilization
In order to declare datatpye and initialize starting value. We can follow the pattern below. Please note that when we access a data member from pointer we use '->' not '.'.
#include<iostream>
using namespace std;
class Obj{
public:
int id;
void inc(){++id;}
};
int main(){
Obj a,b;
Obj *pa,*pb;
pa=&a;
pb=&b;
pa->id=5;
b.id=7;
cout<<"1 a.id="<<a.id<<", b.id="<<b.id<<endl;
pb->inc();
cout<<"2 a.id="<<a.id<<", b.id="<<b.id<<endl;
return 0;
}
output
Running /home/ubuntu/workspace/code71_class_pointer.cpp
1 a.id=5, b.id=7
2 a.id=5, b.id=8
7.2. & and *
operator and & work properly with object. In function input, '' means passing by pointer and '&' means passing by reference.
#include<iostream>
using namespace std;
class Obj{
public:
int id;
};
void swap_id_reference(Obj &a, Obj &b){
int temp=a.id;
a.id=b.id;
b.id=temp;
}
void swap_id_pointer(Obj *a, Obj *b){
int temp=a->id;
a->id=b->id;
b->id=temp;
}
int main(){
Obj a,b;
a.id=0;
b.id=1;
cout<<"1 a.id="<<a.id<<", b.id="<<b.id<<endl;
swap_id_reference(a,b);
cout<<"2 a.id="<<a.id<<", b.id="<<b.id<<endl;
swap_id_pointer(&a,&b);
cout<<"3 a.id="<<a.id<<", b.id="<<b.id<<endl;
return 0;
}
output
Running /home/ubuntu/workspace/code71_class_pointer_init.cpp
1 a.id=0, b.id=1
2 a.id=1, b.id=0
3 a.id=0, b.id=1
7.3. Return Pointer from function
In many cases, return a copy of an object may not appropriate because it take time and memory to copy the object. It is recommended to not copy the object from function to outer scope. The solution is to pass pointer back to the outer scope as the following code.
#include<iostream>
using namespace std;
class Obj{
public:
int id;
Obj(int _id){id=_id;}
};
Obj* last_object(Obj* a, Obj* b){
if(a->id < b->id){
return b;
}else{
return a;
}
}
int main(){
Obj a(5),b(7);
Obj* c=last_object(&a,&b);
cout<<c->id;
}
output
Running /home/ubuntu/workspace/code73_return_pointer.cpp
7
7.4. Pointer with "const"
the keyword 'const' makes the variable to be constant. However, location of the 'const' may have differnct meaning.
7.4.1 no constant
#include<iostream>
using namespace std;
void func( int * p){
int y;
p=&y;
*p=4;
}
int main(){
int x;
int *p=&x;
x=5;//init
func(p);
cout<<x;
}
output
Running /home/ubuntu/workspace/code741_constant_arg.cpp
5
7.4.2 constant at the front (constant value)
#include<iostream>
using namespace std;
void func(const int * p){
int y;
p=&y;
*p=4;
}
int main(){
int x;
int *p=&x;
x=5;//init
func(p);
cout<<x;
}
output
Running /home/ubuntu/workspace/code741_constant_arg.cpp
/home/ubuntu/workspace/code741_constant_arg.cpp: In function ‘void func(const int*)’:
/home/ubuntu/workspace/code741_constant_arg.cpp:6:7: error: assignment of read-only location ‘* p’
*p=4;
^
7.4.3 constant at the back (constant address)
#include<iostream>
using namespace std;
void func( int * const p ){
int y;
p=&y;
*p=4;
}
int main(){
int x;
int *p=&x;
x=5;//init
func(p);
cout<<x;
}
output
Running /home/ubuntu/workspace/code741_constant_arg.cpp
/home/ubuntu/workspace/code741_constant_arg.cpp: In function ‘void func(int*)’:
/home/ubuntu/workspace/code741_constant_arg.cpp:5:6: error: assignment of read-only parameter ‘p’
p=&y;
^