-1

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.

13
  • 6
    Seems like your copy constructor may contain errors. The problem is definitely with objectType. In principal, there is nothing wrong with objectType object2 = object1;. Commented May 7, 2019 at 15:27
  • 4
    Without code there is no way of answering that question. Please provide a minimal reproducible example. Commented May 7, 2019 at 15:28
  • 1
    That is not a copy ctor, please edit your question to contain minimal reproducible example Commented May 7, 2019 at 15:41
  • 2
    You didn't provide a MCVE though Commented May 7, 2019 at 15:49
  • 1
    your copy constructor doesn't initialise stackTop to null so initialize has undefined behaviour Commented May 9, 2019 at 6:52

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.