tags:

views:

172

answers:

2

I am trying to initialize a hashtable. when i do the stock's operator= overload is called. I am not sure what i need to do in this function?

+1  A: 

Your assignment operator is incorrect. The signature needs to be:

stock& operator=(const stock& pRhs)

Secondly, you need to delete first, then capture data. What you have now just delete's NULL. Lastly, you should should s's data, not set to NULL.


Here's how to implement operator=. You need to first make sure any data you hold is free'd. After that, copy the right-hand side.

I don't see a clear() function, but what you want to do is take the code in your destructor, and move it to a public clear() function. Now all your destructor does is call this function, and it let's you clear the hash map at other times too (without duplicating code).

A typical naive method is something like the following: (I'm not going to give you the actual code, just something similar):

// note, T is the type being copied. remember, this is just
// generic helper code, it's up to you to fill in your blanks

// returns a reference, common, allows chaining: a = b = c
T& operator=(const T& pRhs) // rhs stands for right-hand-side
{
    if (this != &pRhs) // avoid self-copy
    {
        clear(); // important! free resources before we copy

        // code to copy pRhs. this
        // is dependent on your class
    }
}

The lines that read "code to copy pRhs" will be replaced by the same code in your copy constructor. But we cannot call the copy-constructor! This means you'll end up copy-pasting that code, and you should never copy-paste code. This is because code duplication is a bad thing. You might try to alleviate this issue by creating a private copy() function, and having both the copy-constructor and operator= call this function instead, but this isn't optimal, and can look messy.

However, there is a better way. It requires you create a swap() function for your class. What this function is responsible for doing is swapping two classes by swapping all their member variables:

#include <algorithm> // for std::swap

// somewhere in the class:
void swap(T& pRhs)
{
    // obviously the variable names depend on your class
    std::swap(someVariable, pRhs.someVariable);

    // and so on for all the variables in the class
}

You can now logically swap two classes by calling classA.swap(classB). You also need a working copy-constructor. What we're going to do is let the compiler make a copy for us, using the copy-constructor, then kill two birds with one stone and swap with it. Here's what I mean:

T& operator=(const T& pRhs)
{
    if (this != &pRhs)
    {
        T temp = pRhs; // copy using copy-constructor
        swap(temp); // swap with the copy
    }
}

And that's it! By making a copy, the compiler will use our copy-constructor; this is what avoids the code duplication.

The second part is to swap with that copy. We give the copy all our stuff, and take it's stuff. We are now the copy (the copy is done).

The last thing is to make sure all our resources get released. Before we were calling clear(), but now it's done automatically. This is because temp needs to be destructed before the function ends. So the compiler does that for us, but remember we swapped our stuff for temp. What this means is the compiler will end up deallocating our old data, while we keep the copied data.

And that is how to implement operator=. Feel free to ask questions, of course.

GMan
You should still be able to add functions. Perhaps make them private?
GMan
Also, if `s->name` is not null-terminated, then `strlen` will likely crash. Make sure your strings are all initialized correctly.
GMan
Your right i probobly could make a function and make them private. I will try that tomorrow.
Corey
"You need to first make sure any data you hold is free'd. After that, copy the right-hand side." - Fails on self assignment. Should be "You need to copy the right-hand side to a temporary variable, then free any pointers you hold, then set your pointers to point to the temporary data."
Bill
Go ahead and edit your question above with the actual definitions of your functions so we can see what you're talking about.
GMan
peterchen
A: 

Whatever i try to do in that function causes a crash. like

name = new char[strlen(s->name) + 1];

why would something like that cause a crash? Whatever i try to do in that function dont work im STUCK

Corey
This should be a comment in response to you answer you are referring to, or an edit to your original question if required.
LeopardSkinPillBoxHat
Is `s` valid? `s->name`? If either of them are null or garbage the `strlen()` function is going to do something bad, possibly crashing the program.
David Thornley