Copy constructor?

Why dose this code not work im trying to use a copy constructor but for some reason the program just hangs if i remove the code at line 31 and if i don't remove the line then the compiler gives this error:

error: '((myClass*)this)->myClass::ptr1' cannot be used as a member pointer, since it is of type 'int*'|
||=== Build finished: 1 errors, 0 warnings ===|



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
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <stdlib.h>

using namespace std;

class myClass{
    private:
    int *ptr1;
    public:
    void set(int n){
        *ptr1 = n;
    }
    // Member functions.
    void display(){
        cout << *ptr1;
    }
    void copy(int *ptr){
        ptr1 = ptr;
    }
    int getLength() const{
        return sizeof(ptr1);
    }
    // Constructors
    myClass(){
        *ptr1 = 0;
    }
    myClass(const myClass &p){
        int length = 0;
        length = p.getLength();
        ptr1 = new int[length];
        *ptr1 = p.*ptr1;
    }
}a;

main(){
    myClass b(a);

    b.set(50);
    cout << "The value of the ptr1 of object b is: ";
    b.display();

    a.set(70);
    cout << endl << "The value of the ptr1 of object ptr is: ";
    a.display();

    cout << endl << "The value of the ptr1 of object b is: ";
    b.display();
}

Last edited on
To have the value of a pointer which is a member, the syntax is *p.ptr1. What you do looks lime a use for method pointer, which is what the compiler is complaining about.

Also, that code will only copy a single integer while new int[length] allocates an array.

Though this has nothing to do with crashing. You simply never allocate a.ptr1. You need to read up on pointers and dynamic memory more.
Fixed that but the program still hangs even after i remove the suspected code? what's wrong with it?
Ah god i cant believe i forgot to allocate the memory for ptr1 thats why the program was not working now that's embarrassing.

The program is working fine now and the copy constructor works well thanks anyway.

now here is a much cleaner version of the code:

if there are any more problems or a better way to do anything here please do tell.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <stdlib.h>

using namespace std;

class myClass{
    private:
    int *ptr1;

    public:
    // Member functions.
    void set(int n){
        *ptr1 = n;
    }

    void display(){
        cout << *ptr1;
    }

    // Constructors
    myClass(){
        ptr1 = new int;
        *ptr1 = 0;
    }
    ~myClass(){
        delete ptr1;
    }

    // Copy Constructor.
    myClass(const myClass &p){
        ptr1 = new int;
        *ptr1 = *p.ptr1;
    }
}a;

main(){
    myClass b(a);

    b.set(50);
    cout << "The value of the ptr1 of object b is: ";
    b.display();

    a.set(70);
    cout << endl << "The value of the ptr1 of object a is: ";
    a.display();

    cout << endl << "The value of the ptr1 of object b is: ";
    b.display();
}
Last edited on
1
2
3
class myClass{
public:
    int ptr1;
Topic archived. No new replies allowed.