static-cast

Downcast in a diamond hierarchy

99封情书 提交于 2019-12-04 15:31:23
问题 Why static_cast cannot downcast from a virtual base ? struct A {}; struct B : public virtual A {}; struct C : public virtual A {}; struct D : public B, public C {}; int main() { D d; A& a = d; D* p = static_cast<D*>(&a); //error } g++ 4.5 says: error: cannot convert from base ‘A’ to derived type ‘D’ via virtual base ‘A’ The solution is to use dynamic_cast ? but why. What is the rational ? -- edit -- Very good answers below. No answers detail exactly how sub objects and vtables end up to be

Static cast vs. dymamic cast for traversing inheritance hierarchies

时光毁灭记忆、已成空白 提交于 2019-12-04 07:01:13
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast is more efficient than using dynamic cast. Example: #include <iostream> #include <typeinfo> using namespace std; class Shape { public: virtual ~Shape() {}; }; class Circle : public Shape {}; class Square : public Shape {}; class Other {}; int main() { Circle c; Shape* s = &c; // Upcast: normal and OK // More explicit but unnecessary: s = static_cast<Shape*>(&c); // (Since upcasting is such a safe and common // operation, the cast becomes cluttering) Circle* cp = 0; Square* sp = 0; // Static Navigation of

casting to void* to pass objects to pthread in c++

青春壹個敷衍的年華 提交于 2019-12-04 03:40:40
问题 I'm a little confused about how to pass an object to the pthread_create function. I've found a lot of piecemeal information concerning casting to void*, passing arguments to pthread_create, etc., but nothing that ties it all together. I just want to make sure I've tied it all together and am not doing anything stupid. Let's say I have the following thread class: Edit: fixed mis-matched static_cast . class ProducerThread { pthread_t thread; pthread_attr_t thread_attr; ProducerThread(const

C++ difference between adding const-ness with static_cast and const_cast of “this” object?

一个人想着一个人 提交于 2019-12-03 17:13:16
问题 As per Scott Meyers, to prevent repetition of code in the const version of a getter and the non-const version of a getter, call the const version of the method from the non-const version: static_cast<const A&>(*this).Methodology(); however , in accidental usage due to an overzealous Visual Assist X Intellisense I typed: const_cast<const A&>(*this).Methodology(); and it worked just fine. What are any and all differences in this case with using a particular cast? IDE in use: Visual Studio 2010.

Accessing subclass members from a superclass pointer C++

六眼飞鱼酱① 提交于 2019-12-03 16:50:47
I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them. The problem is, because these functions are not overloaded, they are not found in Student, so the compiler kicks up a fuss. If I have a pointer to Student, is there a way to get a pointer to the subtype of that Student? Would I need to make some sort of fake cast here to get around the

What wording in the C++ standard allows static_cast<non-void-type*>(malloc(N)); to work?

你说的曾经没有我的故事 提交于 2019-12-03 13:49:02
As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void* -to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void pointer being the same as that of a char pointer, and so on, but it never seems to explicitly say that casting an arbitrary void pointer yields a pointer to the same location in memory, with a different type, much like type-punning is undefined where not punning

Downcast in a diamond hierarchy

我只是一个虾纸丫 提交于 2019-12-03 10:39:42
Why static_cast cannot downcast from a virtual base ? struct A {}; struct B : public virtual A {}; struct C : public virtual A {}; struct D : public B, public C {}; int main() { D d; A& a = d; D* p = static_cast<D*>(&a); //error } g++ 4.5 says: error: cannot convert from base ‘A’ to derived type ‘D’ via virtual base ‘A’ The solution is to use dynamic_cast ? but why. What is the rational ? -- edit -- Very good answers below. No answers detail exactly how sub objects and vtables end up to be ordered though. The following article gives some good examples for gcc: http://www.phpcompiler.org

C++ difference between adding const-ness with static_cast and const_cast of “this” object?

天大地大妈咪最大 提交于 2019-12-03 05:25:41
As per Scott Meyers, to prevent repetition of code in the const version of a getter and the non-const version of a getter, call the const version of the method from the non-const version: static_cast<const A&>(*this).Methodology(); however , in accidental usage due to an overzealous Visual Assist X Intellisense I typed: const_cast<const A&>(*this).Methodology(); and it worked just fine. What are any and all differences in this case with using a particular cast? IDE in use: Visual Studio 2010. Attila Assuming that the type of this is A* , there is no difference. In general const_cast can cast

Why is `decltype(static_cast<T>(…))` not always `T`?

此生再无相见时 提交于 2019-12-03 04:36:18
For the following code, all but the last assertion passes: template<typename T> constexpr void assert_static_cast_identity() { using T_cast = decltype(static_cast<T>(std::declval<T>())); static_assert(std::is_same_v<T_cast, T>); } int main() { assert_static_cast_identity<int>(); assert_static_cast_identity<int&>(); assert_static_cast_identity<int&&>(); // assert_static_cast_identity<int(int)>(); // illegal cast assert_static_cast_identity<int (&)(int)>(); assert_static_cast_identity<int (&&)(int)>(); // static assert fails } Why is this last assertion failing, and static_cast<T> not always

Cast of std::vector of same parameter type but with different constant qualifier

三世轮回 提交于 2019-12-02 13:20:01
the question is pretty simple, is it in general safe a static cast (or some other cast) from std::vector< Foo > to std::vector< const Foo > binary-wise, i don't see why the native types would differ, after all, the const is a language constraint that should not affect the size of the element, or so i think can i do std::vector< const Foo >& someFunc() { std::vector< Foo >& ref = ... return *reinterpret_cast<std::vector< const Foo >*>(& ref); } and not worry that this will sink someone's boat? or is this unsafe in general? Bowie Owens Ignoring that you said std::vector for the moment and