I made a private API that assumes that the address of the first member element in the class will be the same as the this-pointer class ... so a member object can trivially get a pointer to the object that it is a member without having to explicitly point pointer.
Given that I am ready to make sure that the container class will not inherit from any superclass, will not have any virtual methods, and that the member object that does this trick will be the first declared object declared, this assumption is true for any C compiler ++, or do I need to use the offsetof () operator (or similar) to guarantee correctness?
In other words, the code below does what I expect under g ++, but will it work everywhere?
class MyContainer { public: MyContainer() {} ~MyContainer() {}
source share