optimization - C++: Fastest way to initialize class object to 0 -


let's have object initialize 0 in constructor. every bit instanced object occupies should 0 without exception, including non-pod members, ignoring personal default constructors completely.

is possible in c++? , if so, there way can done @ least fast initializing each member 0 through initialization list (when allowed)?

(there's obvious pitfalls i'm curious; please assume have not-awful reason!)

there no "magic" syntax achieves this.

if class has no virtual table, use memset(this, 0, sizeof(* this)), not recommended.

you try play offsetof pinpoint address of first member, , erase there on. bit better memseting whole thing, still making me uncomfortable:

// example non-pod type. class b { public:     b() : b(0xdeadbeef) {}     int b; };  class monstrosity { public:     monstrosity()     {         size_t offset = offsetof(monstrosity, a);         uint8_t *erasestart = (uint8_t *)this + offset;         memset(erasestart, 0, sizeof(monstrosity) - offset);     }      virtual int foo() { return 0; }      int a;     b b; }; 

if if members has virtual table, you're practically screwed.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -