Reinitialize Object In-Place

#include <iostream>
#include <cstdint>
#include <new>

using namespace std;

// Write your struct and placement new logic here
typedef struct{
    int level;
}Mode;
int main() {
    int firstLevel, secondLevel;
    cin >> firstLevel >> secondLevel;

    // Write your in-place reinitialization logic here
    unsigned char buff[sizeof(Mode)];
    Mode* m = new (buff) Mode();
    m->level = firstLevel;

    m->~Mode();
    m = new (buff) Mode();
    m->level = secondLevel;
    cout<<m->level;
    m->~Mode();
    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

3 9

Expected Output

9