-4
class a{
    int *var = new int;
public:
    //constructor and destructor
    a(int a):var(new int(5)){}
    ~a() {delete var;}


    int get() const {return *var}
    //overload of + oporator
    a operator+(const a & rhs){return a(*var+rhs.get()
    //overload of ++ operator
    a a::operator++ (int)
}

a a::operator+ (const a & rhs) {
    return a(*itsRadius + rhs.get());
}
a a::operator++ (int){
    a temp(*this);
    *itsRadius= *itsRadius+1;
    return temp;}

}

now when i do like this:

a c(10),d,g;
g=c+d;

i get g = to some address instead of 15 . why is that?

and when i fo c++ i get an error (in the distructor when he try to delete),why is that?

adam smith
  • 35
  • 5
  • Why do you have a int-pointer instead of just a int? – tkausl Jun 23 '16 at 12:53
  • 1
    This code is crappy. Too difficult to read. – Boiethios Jun 23 '16 at 12:54
  • @tkausl I think it is as an example to understand how to create new object with allocated resource inside it. – Boiethios Jun 23 '16 at 12:55
  • 1
    `int *var = new int;`, you initialize `var` but `*var` is unitialized, so `d.get()` can be anything... – Holt Jun 23 '16 at 12:56
  • Okay, well. The problem isn't the `operator +` but the default copy-constructor. And you have a memory-leak there. – tkausl Jun 23 '16 at 12:56
  • This doesn't even compile. Would be great if you'd show us your real code instead of some buddy dummy-code... – tkausl Jun 23 '16 at 12:59
  • i edit the code so it will be clearer , @Holt whay when i ceclear `int *var = new int;` it's not declear var? new return a pointer so *var should be the contant of this memory pointer , @tkausl what is the problem of the copy constructor? and where is the memory leak? i did delete in the destructor – adam smith Jun 23 '16 at 13:04
  • @adamsmith `int *var = new int` set the default value for `var`, i.e. if the default constructor is used (as it is the case for `d`), a memory location will be allocated (and be accessible using `*var`), but the content of this memory location will be undefined since you did not initialize it. Use `int *var = new int(0);`. – Holt Jun 23 '16 at 13:06
  • @Holt but i have initialization in the constructors (1 with difolt 5 and 1 with any number i want) – adam smith Jun 23 '16 at 13:15
  • @adamsmith Either I have big problem, either you did not show us your real code because in the code you post you only have one constructor that takes a number and that default `*var` to 5... – Holt Jun 23 '16 at 13:17
  • sorry you right i forgat to add the outher constructor ti the que. – adam smith Jun 23 '16 at 13:33

2 Answers2

1

Here is a working example. You need also to read about the rule of three:

#include <iostream>

class Int
{
    int *_value;

public:
    Int(int value) : _value(new int(value))
    {
    }
    ~Int()
    {
        delete _value;
    }
    Int(Int const &rhs) : _value(new int(*rhs._value))
    {
    }
    Int & operator=(Int const &rhs)
    {
        *_value = *rhs._value;
        return *this;
    }

    Int operator+(Int &rhs) const
    {
        return Int(*rhs._value + *_value);
    }

    operator int() const
    {
        return *_value;
    }
};

int main(void)
{
    Int a(10),
        b(32),
        c(a + b);

    std::cout << c << "\n";
    a = c;
    std::cout << a << "\n";
}

And also, it is a bad idea to use raw C pointers in C++. Read about the std::unique_ptr.

Community
  • 1
  • 1
Boiethios
  • 38,438
  • 19
  • 134
  • 183
0

o.k. so i solve the problem , this is the working code :

class SimpleCircle {

int *itsRadius;

public:

SimpleCircle():itsRadius(new int(5)) { cout << "constructor initialized" << endl;}
SimpleCircle(int num) { itsRadius = new int(num); cout << "constructor" << endl;}
SimpleCircle(const SimpleCircle  &rhs) : itsRadius(new int(*rhs.itsRadius)){ cout << "copy constructor" << endl; }
~SimpleCircle(){ delete itsRadius; cout << "destructor" << endl;}


//perfect
int get() const {return *itsRadius;}
void set(int num) { *itsRadius = num;}
//-------


//plus operator
SimpleCircle operator+(const SimpleCircle &);

//inc operator
SimpleCircle operator++();
SimpleCircle operator++(int);

//= operator
SimpleCircle & operator=(const SimpleCircle &);
};




SimpleCircle SimpleCircle::operator+ (const SimpleCircle & rhs) {
return SimpleCircle(*itsRadius + *rhs.itsRadius);
}

SimpleCircle SimpleCircle::operator++() {
int a = *itsRadius;
++a;
*itsRadius=a;
return *this;
    }

SimpleCircle SimpleCircle::operator++ (int){
SimpleCircle temp(*this);
*itsRadius= *itsRadius+1;
return temp;
}


SimpleCircle & SimpleCircle::operator= (const SimpleCircle & rhs) {
if (this == &rhs)
    return *this;

*itsRadius = *rhs.itsRadius;
return *this;
  }

int main()
{

SimpleCircle a;
cout << a.get() << endl;

SimpleCircle b(15);
cout << b.get() << endl;

    SimpleCircle c = a + b;

cout << "a: "<< a.get() << endl;
cout << "b: " << b.get() << endl;
cout << "c: " << c.get() << endl;

a++;
cout << "a: " << a.get() << endl;
++a;
    cout << "a: " << a.get() << endl;

now the reason in the former code i had 2 problem (that was 1 becoase of the outher) 1 c was equal to some garbege instead of a number 2 the program break in the end destructor

the reason was i forgat o add a operator= , so it didn't know how to treat to :

c=a+b;

after i fixed it , all come together nicely

adam smith
  • 35
  • 5