Pointer as private member data

Hello. I'm currently having issues with a program that has a pointer of type double as a private member data of a class. I took a snippet of the code and created a shorter example that has the same problem. Here are the files.

mountain.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class point
{
public:
    point () {x=0;}

private:
        double x;
};

class pointPtr
{
public:
    //pointPtr(){}
    pointPtr(){*x=0;}
    //pointPtr(double a){*x=a;}
    /*~pointPtr()
    {
        delete  x;
    }*/
private:
    double *x;
};


And here is mountain.cpp
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
#include <iostream>
#include "mountain.h"

using namespace std;

int main()
{
	cout << "test1" << endl;

	point *ptr1;
	ptr1= new point;

    cout << "test2" << endl;;

	pointPtr *ptr2;

	ptr2= new pointPtr;

	cout << "test3" << endl;

	delete ptr2;

	cout << "test4" << endl;

}


Here is the output.
test1
test2
test3

Process returned -1073741819 (0xC0000005)   execution time : 2.855 s
Press any key to continue.


When I create a point pointer and then use that to make a new point, everything seems to work fine (hence outputting test2). However, I'm having an issue with my pointptr class. Instead of having a double as the private data, I use a double pointer. I repeat what I did with the other class but my program crashes after outputting test 3 (test 4 isn't displayed). I tried deleting the pointer but that still doesn't work. Any help would be greatly appreciated!

Edit: I added a cout statement to my destructor in the pointptr class.
1
2
3
4
5
6
7
8
9
10
11
12
13
class pointPtr
{
public:
    //pointPtr(){}
    pointPtr(){*x=0;}
    //pointPtr(double a){*x=a;}
    ~pointPtr()
    {
        cout << "deconstructor";
    }
private:
    double *x;
};


and my new output is
test1
test2
test3
deconstructor


but the program freezes.
Last edited on
Hi there,

from what I can see you have never instantiated your pointer in the pointPtr class. Try the below, it should work for you - if it doesn't let me know...
1
2
3
4
5
6
7
8
class pointPtr
{
     public:
          pointPtr() : x ( new double ) { *x = 0; }
          ~pointPtr() { if(x != NULL) delete x; }
     private:
          double* x;
};

Remember the rule of 3 though if your going to do this to avoid further errors in the future...
http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)

I hope this helps,

Kind Regards,

Phil.
Philip,

You are a saint! Thank you so much. The code you gave me worked with the snippet and I could apply it to my program. Now I can finally move on.

As far as the rule of 3, am I missing one of them from my class? I know the "~pointPtr()" is the destructor. What is the name for the code you gave me? Is that the copy constructor?

Edit: I see that you used the word instantiate. I'll look up what exactly that means. Does instantiating a pointer (or object) take care of the copy constructor and assignment operator or are those two just absent from my code?
Last edited on
Not having a copy constructor/assignment operator are a different problem. Without them, the default one will preserve the pointer value of x. This is bad as then the first object to go out of scope will delete it, invalidating the second object's pointer.
Topic archived. No new replies allowed.