0

I created a class Event

class Event {
    char m_event_desc[DESC_LENGTH + 1]; // description of the event 
    unsigned int m_time; // time when the event starts
    unsigned int getHour();
    unsigned int getMinute(unsigned int hour);
    unsigned int getSecond(unsigned int hour, unsigned int minute);
    
public:
    Event(); // default constructor 
    void display();
    void set(const char* address = nullptr);

};

and wants to know the difference between

void Event::set(const char* address) {
    if (address != nullptr && address[0] != '\0') {
        // start new event and store that in the current Event object
        m_event_desc[0] = '\0';
        strcpy(m_event_desc, address);
        m_event_desc[strlen(address)] = '\0'; // put null terminator to avoid any additional value after 
        m_time = time;
    }
    else {
        Event(); // reset the state as the default value 
    }

}

and

void Event::set(const char* address) {
    if (address != nullptr && address[0] != '\0') {
        // start new event and store that in the current Event object
        m_event_desc[0] = '\0';
        strcpy(m_event_desc, address);
        m_event_desc[strlen(address)] = '\0'; // put null terminator to avoid any additional value after 
        m_time = time;
    }
    else {
                // reset the state as the default value 
        m_event_desc[0] = '\0';
        m_time = time;
    }

}

The latter worked but I would like to know why the former does not work. I would appreciate if anyone can help me out with this.

5
  • 2
    You cannot call constructor. Constructor is called for you when you create an object and only then. Event(); creates a temporary unnamed object and immediately discards it. It cannot modify this object from set function call. Commented Jan 21, 2024 at 14:25
  • Event() is not actually calling the constructor. Is a constructor a function and is it possible to call a constructor. In the latter case, you're explicitly resetting the member variables while in the latter Event() has no effect on the current object(and its member therefore) Commented Jan 21, 2024 at 14:38
  • 1
    "The latter worked..." What does "worked" mean? We need more information like the driving code and a minimal reproducible example Commented Jan 21, 2024 at 14:39
  • 2
    Do you want: *this = Event();? Commented Jan 21, 2024 at 14:58
  • Welcome @Himawari.Ksm👋🏾 You'll get more answers if you include the code that calls this class as well as the error output you get when it "does not work" and the output you get when "the latter worked" as @user12002570 suggested. Commented Jan 22, 2024 at 20:22

1 Answer 1

0

The former Event() creates a temporary object of type Event which is then discarded. It has no effect on the current instance this. So we don't see any changes in the member variables in this case. From constructors reference:

Constructors have no names and cannot be called directly.

So constructor can't be explicitly called unlike destructors which can be explicitly called.

Event(); //this has no effect on current instance
*this = Event();  //this has effect on current instance as we're assigning to *this

On the other hand in the latter case, you're explicitly resetting the member variables so you see the changes in those as expected.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.