0
class Examp{
    private:
        float *r;       
    public:                 
        Examp(){
            //*r=10.0f;         
        }               
        ~Examp(){
            delete r;
        }                       
        Examp(float r){
            *(this->r)=r;               
        }   
        float circlearea(){
            return 3.14*(*r)*(*r);
        }       
        Examp operator +(Examp &e){                     
            Examp ex;   
            *(ex.r)=*(this->r)+*(e.r);      
            return ex;
        }
        void show(){    
                    std::cout<<"Radius :"<<*r<<std::endl;
        }
};
Examp *e1=new Examp(10);
Examp *e2=new Examp(5);
Examp *e3=new Examp;
e3=e1+e2;
e1->circlearea();
e2->circlearea();
e2->circlearea();

when I'm executing this code the output is not shown correctly if I try to find the area and sometimes the code below the above object is also not getting executed

I should get the area as my output

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Zebra
  • 25
  • 4

2 Answers2

2

A float* should point to a float object. delete r in the constructor suggests that there should also be a new float somewhere.

In modern C++, we rarely use float* to hold the results of new float. In your example for instance, it would be much better to just have a member float r. If you would use pointers, use smart pointers and skip the hassle of new/delete. use e.g. std::shared_ptr<float>.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

Suppose you ask me wether you can store your bike in my garage. I say: "Yes of course, here you got a piece of paper with my adress written on it." Thats:

 int garage;              // my garage
 int* address = &garage;  // i tell you my adress
 
 *address = 42;           // you use the adress to find my garage and put your bike inside

So far so good.

Now consider same story, but I tell you "Yes of course, here you got a random piece of paper, I just randomly picked from my desk." Thats:

 int* address;     // uninitialized pointer
 
 *address = 42;    // undefined behavior !!!

The analogy is broken, because neither will you explode nor will your harddrive get formatted while you search my garage based on a useless sheet of paper. Though, undefined behavior can do that. Reading an uninitialzed value is undefined. Dereferencing an invalid pointer is undefined.

In your example there is no need to use a pointer. It makes the code more error prone for no apparent reason. If you actually do need a dynamically allocated member that is owned by the class, you should use a std::unique_ptr instead of raw pointers. Raw new and delete are to be avoided.

TL;DR: Your member is a pointer to a float. Unless you actually have a float somewhere, there is no float you could assign to.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • ok if I allocate memory for my float variable here to deallocate it `delete r ` is enough rit – Zebra May 11 '21 at 12:24
  • 1
    @Zebra no, actually thats far from being enough. You should read about the [rule of 3/5](https://en.cppreference.com/w/cpp/language/rule_of_three) and make sure to read till the end where they explain the rule of 0 – 463035818_is_not_an_ai May 11 '21 at 12:25
  • If I use a unique_ptr to create my objects then for 'e3=e1+e2;' error is being thrown ' no match for 'operator+' ' – Zebra May 11 '21 at 12:52
  • @Zebra I dont know what is wrong in code I dont see. Works here: https://godbolt.org/z/b44jEa41M. However, don't write code like this. You will realize when you need a unique pointer not here. From the top of my head I don't know any good use case for dynamically allocating a single `float`. – 463035818_is_not_an_ai May 11 '21 at 14:03
  • If I have `Examp e5=e4` after my `e3=e1+e2` output is being thrown `use of deleted function 'Examp::Examp(const Examp&)` – Zebra May 11 '21 at 14:39
  • @Zebra you cannot copy `unique_ptr`s you can move them. This however, is not a big drawback, because in your original code `Examp` cannot be copied as well (without causing big trouble) – 463035818_is_not_an_ai May 11 '21 at 14:40