Category : static-cast

Consider the following program: #include <iostream> class Pet {}; class Cat: public Pet { public: void meow() const { std::cout << "Meow!" << std::endl; } }; int main() { Cat myCat; Pet& myPet = myCat; static_cast<Cat&>(myPet).meow(); // Undefined behavior? Pet myOtherPet; static_cast<Cat&>(myOtherPet).meow(); } On my machine, the output is: Meow! Meow! As expected, myPet is ..

Read more

#include <iostream> int main() { double val = -400.0; const double constVal = -400.0; std::cout << val << std::endl; std::cout << static_cast<unsigned>(val) << std::endl; std::cout << constVal << std::endl; std::cout << static_cast<unsigned>(constVal) << std::endl; return 0; } Output: -400 4294966896 -400 0 wtf mate? A moderate amount of Googling shows nothing on this matter. Source: ..

Read more

I am using https://cppinsights.io/ . Following is the code (default example) #include <cstdio> int main() { const char arr[10]{2,4,6,8}; for(const char& c : arr) { printf("c=%cn", c); } } About cppinsights C++ Insights is a clang-based tool which does a source to source transformation. Its goal is to make things visible, which normally and intentionally ..

Read more

Sorry if the question title makes no sense, but I’m not sure how to succinctly describe the problem I’m trying to solve. Here’s the issue: I’m working with a C++ library that makes heavy use of a class which we’ll call Base This library has several different child classes that inherit from Base. We’ll call ..

Read more

I have a struct with several (virtual) bases. From one of these bases’ constructor I static_cast to the most derived type, and from there try access a member of another base (which has been initialised before). The adress doesn’t match with the result when accessing from the most derived struct’s constructor directly. Weirdly (at least ..

Read more