Copy constructor woes

can someone tell me why the copy constructor does not seem to be called when the code below is run?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Person{
    public:
        Person(string * n){
            name = new string(*n);
            }

//Copy constructor
    Person(const Person &p){
    cout <<"copy constructor called"<<endl;
        name = new string(*p.name);

        }

~Person(){
        delete name;
    }

protected:
        string * name;
};



int main(){

string s1 ="Jimmy Bob";
string *ps = &s1;
Person p1(ps);

s1="voodoo";
ps = &s1;
Person p2(ps);
//WHY DOESN"T NEXT LINE CAUSE COPY CONSTRUCTOR TO BE CALLED?
p2 = p1;
}
You don't use copy - constructor there. You use assignment operator.
There are 2 ways to use copy constructor , that i know.
First way ;

 
Person p2 = p1 ;


second way ;
 
Person p2( p1 );


NOTE THAT
 
Person p2 = p1 ;

and
1
2
Person p2 ;
p2 = p1 ;

are different.
That line calls the assignment operator ( = ).
Copy constructor is called on initialization: Person p2 = p1;
thanks!
Topic archived. No new replies allowed.