c++ - upcast of nullptr through class with multiple inheritance -


the problem implicit cast of nullptr, second super class, of class multiple inheritance results (at least llvm 7.0.2) in adjustment being applied nullptr. pointer no longer null (if null checks being performed in methods of super class) can result in crash (i guess technically undefined behaviour).

here's minimal example:

#include <iostream>  inline bool pointerisnotnull(const void* ptr) { return ptr != nullptr; }  class intvalue {     public:         intvalue() { }         int getintvalue() { return pointerisnotnull(this) ? value : 0; }     private:         int value; };  static const char* nullptrchar = "nullptr";  class charvalue {     public:         charvalue() { }         const char* getcharvalue() { return pointerisnotnull(this) ? value : nullptrchar; }     private:         char* value; };  class foo : public intvalue, public charvalue {     public:         foo() { }         double getdoublevalue() { return pointerisnotnull(this) ? value : 0; }     protected:         double value; };  int main(int argc, const char * argv[]) {     foo* foo = nullptr;      std::cout << foo->getintvalue() << std::endl;      charvalue* charvalue = foo;     std::cout << charvalue->getcharvalue() << std::endl;      std::cout << foo->getcharvalue() << std::endl; } 

my question this: is there way check kind of shenanigans without manually checking nullptr before calls second superclass?

you know, there elegant way (maybe in second superclass) assure me i've caught possible examples of behaviour?

edit: yes, know calling member functions nullptr isn't modern practice. thought (until posted question) used accepted practice , in case i'm constrained standards don't have control over. so, with assumption calling member function on nullptr enter correct function, there elegant solution problem?

foo->getintvalue() undefined when foo null pointer, makes entire program undefined.
is, dereferencing undefined, , program doomed before reaches check.

there no point in checking whether this null, since compiler free assume isn't (if were, program undefined, compiler can whatever wants).


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -