I have overloaded the assignment operator for a particular class and after that I soon found out a problem.

During the declaration of a class object, if I initialize it with another object that already exists,

       objectType object1;
       objectType object2 = object1;

the program will terminate with a message of program.exe has stopped working during the run.

However, if I separate the declaration and initialization steps,

        objectType object1, object2;
        object2 = object1;

it will be working fine.

If we can do this with simple data type,

        int x = 6;
        int y = x;

Why can't we do this with class object? I hope my question is clear and I have tested this with different computers with the same .exe has stopped working result.

Edited: Here's my code. The class itself is a stack.

    #include <iostream>
    using namespace std;

    struct nodeType
    {
        int info;
        nodeType *link;
    };

    class objectType
    {
        public:

            const objectType& operator=(const objectType& otherObject)
            {
                if(this != &otherObject)
                {
                    copyObject(otherObject);
                }
                return *this;
            }

            void initialize() //Initialize the stack
            {
                nodeType *temp; 

                while(stackTop != nullptr)
                {
                    temp = stackTop;
                    stackTop = stackTop->link;
                    delete temp;
                }
            }

            objectType(const objectType& otherObject) //Copy constructor
            {
                copyObject(otherObject);
            }

            objectType() //Constructor
            {
                stackTop = nullptr;
            }

            ~objectType() //Destructor
            {
                initialize();
            }

        private:
            nodeType* stackTop;

            //Copy function to implement copy constructor and overload assignment operator
            void copyObject(const objectType& otherObject)
            {
                initialize();

                if(otherObject.stackTop != nullptr)
                {
                    nodeType *current, *last, *newNode;

                    current = otherObject.stackTop;

                    stackTop = new nodeType;
                    stackTop->info = current->info;
                    stackTop->link = nullptr;

                    last = stackTop;
                    current = current->link;

                    while(current != nullptr)
                    {
                        newNode = new nodeType;
                        newNode->info = current->info;
                        newNode->link = nullptr;

                        last->link = newNode;
                        last = newNode;

                        current = current->link;
                    }
                }
            }
    };

    int main()
    {
        objectType object1;
        objectType object2 = object1;
        return 0;
    }

After I tested it with debugger, I found out the problem was the destructor. As I know, the destructor is called when the object goes out of scope. Does the object go out of scope in this case?

P.S. Can this code be considered as MCVE now? And yeah reproducing the code really helps me to find out the real root of the problem.

1 Answers

1
Lightness Races in Orbit On

This means your objectType has a broken copy constructor implementation, but a working copy assignment operator.

You can prove to yourself that this is not a feature of classes, by doing it with any other type (e.g. std::string).

Unfortunately we cannot tell you how it is broken, because we can't see it; fire up your debugger and get cracking!