If you create two instances of that class, they will have two different p
members each containing a different address of different regions in memory, yes.
You can probably tell if you run your program: it will display 3 and 5, if the pointer was the same, it would have displayed 5 twice.
Edit: as asked, some additional explanation (and a summary of what have been said in the comments)
First of all, your operator=
is leaking memory, instead you should remember to release the memory already allocated in p before reassigning it:
void operator=(const A &k){
delete p;
// as JoshuaGreen states in the comments, you can set p to nullptr
// here, that way, if new fails and throws, p will be set to nullptr
// and you'll know it doesn't contain anything (you'll have to test
// it in other methods to benefit from this modification though,
// but it will be safer)
p = nullptr;
p = new int;
*p=*(k.p);
}
Although in this specific case, you could just avoid reallocating:
void operator=(const A &k){
// p = new int; // not needed
*p=*(k.p);
}
Now, that was indeed important to overload the assignment operator=
(and you actually forgot to overload the copy constructor), let's see what would have happened if this assignment operator=
wasn't defined.
Your class would instead look like this:
#include<iostream>
using namespace std;
class A{
public:
int *p;
A(){
p=new int;
}
void set(int b){
*p=b;
}
int get(){
return *p;
}
~A(){
delete p;
}
};
But actually, the compiler will generate an implicitly defined default copy constructor and default assignment operator= for you. Of course you don't see their implementation, but they would behave exactly the same as if the class was defined like this:
#include<iostream>
using namespace std;
class A{
public:
int *p;
A(){
p=new int;
}
A(const A& other) :
p(other.p) {} // ! we're copying the pointer instead of reallocating memory
void set(int b){
*p=b;
}
int get(){
return *p;
}
A& operator=(const A& other){
p = other.p; // same here!
}
~A(){
delete p;
}
};
When you're class deals with dynamically allocated memory, that's bad. Let's see what would happen in your main:
int main()
{
// allocate a new pointer to int, let's call it p
A obj;
// set the content of p to 3
obj.set(3);
// allocate a new pointer to int, let's call it p1
A obj1;
// /!\
// instead of copying the content of p to the content of p1, we're
// actually doing p1 = p here! we're leaking memory AND the two
// objects point on the same memory!
obj1=obj;
// set the content of p1 to 5, but p1 is now equal to p because of
// the bad assignment, so we're also setting p's content to 5
obj1.set(5);
// print the content of p (5)
cout << obj.get() << endl;
// print the content of p1 (5)
cout << obj1.get() << endl;
}
// delete the contents of p and p1 /!\ we're actually deleting the same
// allocated memory twice! that's bad
That's why you have to redefine the "big three" (copy constructor, copy assignment operator, and destructor)