mardi 10 octobre 2017

Code throws exception

    #include "stdafx.h"
    #include <iostream>
    #include<conio.h>
    #include<cmath>
    using namespace std;

    class IntDynamicPointer
    {public:
    int *pointer;
    IntDynamicPointer()
    {
    pointer = NULL;
    }
    void AllocMem()
    {
    pointer = new int;
    }
    void setValue(int & val)
    {
    pointer =& val;//Exception
    }
    int getValue() 
    {
    cout << pointer<<endl;
    return *pointer;
    }
    ~ IntDynamicPointer()
    {
    delete pointer;
    pointer = NULL;
    }
    };
    void main()
    {
    int a = 5;
    IntDynamicPointer o1;
    o1.setValue(a);
    cout<<o1.getValue();
    _getch();
    }

I am just a beginner in programming with pointer the code above throws an exception in the function setValue please explain to me why? I think it is a bad pointer but please explain to me if it is so.

Aucun commentaire:

Enregistrer un commentaire