Franklinprogs
Franklinprogs

Reputation: 21

c++ bad class instance

I have this code and the exercise is to find out which error will occur if I build an Instance of my Class and delete it afterwards. I can't find the fault in the definition of this class so maybe you can help me. Here's the code:

class BadClass{
public:
BadClass(){
    p = new double;
}
~BadClass () {}

    double getValue() {return *p;}
    void setValue(double v) {*p = v;}

private:
    double* p;
};

Upvotes: 0

Views: 140

Answers (1)

APerson
APerson

Reputation: 8422

You call new double in the constructor without a corresponding delete p call in the destructor.

This will result in a memory leak.

Upvotes: 2

Related Questions