2


Is it okay to use placement new for automatic objects? Please consider the following example:

class Button
{
public:
    Button() {  }
    virtual ~Button() {  }

    // and a lot of members
};

class Screen
{
public:
    Screen() {  }
    virtual ~Screen() {  }

    Button submit_btt;

    void doStuff()
    {
        // ...
        submit_btt.~Button();
        new(&submit_btt) Button();
        //...
    }

    // and a lot of members
};

void process(void)
{
    Screen myObj;

    //...
    myObj.doStuff();
    //...
}

This pseudo code represent situation which has been met when working GUI framework.
What are your thought about this code? Are there any insecurities? What can go wrong? Will myObj and its' members be properly destroyed (all member destructors called and all other operations) after process() function?

  • 2
    Why would you want that? If you correctly overload operators, then `submit_btt = Button()` correctly calls the destructor of the old button and a constructor of the new one. Actually it should even happen without overloading `operator=`. – Matthias247 Mar 30 '17 at 12:31
  • @Karolis: What is the use of "new(&submit_btt) Button();", you will not be able to do something with it. – Sumeet Mar 30 '17 at 12:31
  • you just want a assignment operator. Or a better GUI framework which support change property of button. – apple apple Mar 30 '17 at 12:32
  • The purpose of that would be to reset the object completely. Of course, there are other ways. – Karolis Milieška Mar 30 '17 at 12:33
  • 1
    This looks like an evil hack and terrible coding practice and you should be forced to do a week of customer support because of that, but as far as I can see it is not forbidden by the C++ standard. – nwp Mar 30 '17 at 12:34
  • 1
    your `Button submit_btt;` is not a polymorphic button anymore. – apple apple Mar 30 '17 at 12:37
  • @appleapple, why? – Karolis Milieška Mar 30 '17 at 12:39
  • @KarolisMilieška Because value objects in C++ are never polymorphic. – Konrad Rudolph Mar 30 '17 at 12:48
  • @appleapple, this look like other topic, but disagree. Can you please provide more info on what you are saying? Why it can't be polymorphic while being local (value)? – Karolis Milieška Mar 30 '17 at 12:51
  • It's [object slicing](http://stackoverflow.com/q/274626/5980430). – apple apple Mar 30 '17 at 12:56
  • @KarolisMilieška See my comment. It’s got nothing to do with being local. But value objects (non-pointers, non-references) cannot be polymorphic, full stop. Consider this simple problem: polymorphic objects may be of different size (subclasses can add member variables), but the space allocated for a value is fixed. – Konrad Rudolph Mar 30 '17 at 12:56
  • @KonradRudolph, of course, but when doing polymorphism we always assign and share pointers and references, never assigning by value. Allocated space will always be sizeof that class. You never "add" more memory or "extending" an object, but creating new instance of appropriate class. Am I wrong? – Karolis Milieška Mar 30 '17 at 13:04
  • @KarolisMilieška You’re correct, but that’s why polymorphism doesn’t work with variables referring to values: there’s no space to create a new class instance of a different size at `&submit_btt`. It refers to a memory location with a fixed allocates size of `sizeof(Button)`. – Konrad Rudolph Mar 30 '17 at 13:09
  • @KonradRudolph, please proide an example of that for NOT a value variables. Also, I didn't mention that I am trying to create bigger object at fixed smaller location. One more thing: indeed, it would be possible to create new instance of `button` derivative and put its' base at `&submit_btt`. But still, I have never seen anything like assigning polymorphic objects by value even when working with dynamic memory. Please show me. :) – Karolis Milieška Mar 30 '17 at 13:16
  • @KarolisMilieška `Button*`. `std::unique_ptr – Konrad Rudolph Mar 30 '17 at 13:20
  • @KonradRudolph It is not "by value". – Karolis Milieška Mar 30 '17 at 13:22
  • @KarolisMilieška That’s my whole point. **You cannot do polymorphism by value**. – Konrad Rudolph Mar 30 '17 at 13:25
  • In this case you only passing around references. You never do value = value. Except copy-constructors or assignment. – Karolis Milieška Mar 30 '17 at 13:26

0 Answers0