0

I made a class str for practice, and I use operator = to assign an object into another. I make like this.

#include <string.h>

class Str{
private:
    char *str;
    int len;
    int num;  
public:
    Str(int leng);
    Str(char *neyong);
    ~Str();

    int length(void);
    char *contents(void);
    int compare(Str a);
    int compare(char *a);
    void operator=(char *a);
    void operator=(Str a);
};

Str::Str(char *neyong){
    len = strlen(neyong);
    str = new char[len + 1];
    strcpy(str, neyong);
}
Str::~Str(){
    delete[] str;
}
int Str::length(void){
    return len;
}
char* Str::contents(void){
    return str;
}
void Str::operator=(Str a){
    len = a.length();
    str = new char[len+1];
    strcpy(str, a.contents());
}

(I skip some functions for ease of reading)

And I execute this like below code.

Str a("hahahaha"), b("hihihihihihi");

cout << a.contents() << endl;
cout << b.contents() << endl;

a = b;

cout << b.contents() << endl;
cout << a.contents() << endl;   

Problem: When I assign a = b, then destructors of b is automatically called. Probably b.~Str() erases all its content and hence b.contents() doesn't return proper value.

How can I solve this problem????

iammilind
  • 68,093
  • 33
  • 169
  • 336
sonagi
  • 39
  • 1
  • You also need to implement the copy constructor. You make a copy in the assignment operator because the parameter is a `Str` value. – juanchopanza Apr 10 '16 at 08:36
  • 1
    or you can pass it by reference `void Str::operator=(const Str& a)` – Mike Apr 10 '16 at 08:37
  • 1
    @Mike Sure. Sometimes it makes sense to pass by value (for *copy-and-swap*), but not in this case. But the copy constructor is needed. Without it (and without disabling copy), the class is a bug. – juanchopanza Apr 10 '16 at 08:39
  • BTW, the assignment operator has a memory leak. You also need to fix that. – juanchopanza Apr 10 '16 at 08:40
  • of course the copy constructor is needed to make the class have basic functionality, I meant just to get around the error. – Mike Apr 10 '16 at 08:41
  • I did not read all your post. The destructor is called because you do not pass the `Str a` by reference. Also, you should delete the `str` member before assigning it new memory. – zdf Apr 10 '16 at 08:44
  • thanks a lot !!!!! I solve the problem immediately !!! – sonagi Apr 10 '16 at 08:51
  • Related: [What is The Rule of Three?](http://stackoverflow.com/a/4172724/514235). @juanchopanza, I am reopening this Q, because for laymen visitors, it creates confusion when they read "Exact Duplicate" of above Q. It will be good, if someone answers the specific Q. To me it appears as a coding error rather than learning the concept (i.e. "rule of three"). – iammilind Apr 10 '16 at 08:59
  • @iammilind The copy constructor is not implemented. If it were, the only remaining error would be the memory leak, which is not the problem OP is reporting. The main coding error *is* the fact that the rule of three has not been followed. I am not sure what other coding error you're referring to. – juanchopanza Apr 10 '16 at 09:01
  • @juanchopanza, there are more problems apart from copy ctor & memory leak. The syntax of `operator= (Str)` and `operator= (char*)`, are wrong. Should be `operator= (const Str&)` and `operator= (const char* const)` respectively. Same for `compare(char*)`. The `Str::str` should be assigned to `nullptr` within class itself. There is no `nullptr` checks in methods with arguments as `char*`. There can be few more. The "rule of 3" question is a wiki kind of article; probably for bit more knowledgeable audience. But from all above, the OP looks bit novice. It will be good if all issues are addressed. – iammilind Apr 10 '16 at 09:17
  • Also when you go out of scope, that also calls the destructor. – maxadorable Apr 10 '16 at 09:24
  • @ZDF the `delete` should come afte the `new`, so that if the new throws then the string does not break – M.M Apr 10 '16 at 09:27
  • @M.M. The `delete` is nowhere. That's what I meant. – zdf Apr 10 '16 at 09:32
  • @iammilind There is nothing wrong with that syntax. But OK, there are many bugs, beyond not following the rule of three. – juanchopanza Apr 10 '16 at 09:40
  • @sonagi Your code is full of bugs. I suggest you learn some more C++ and tackle a simpler problem first. – juanchopanza Apr 10 '16 at 09:42

0 Answers0