Questions tagged [static-cast]

A C++ cast operator to convert from one type to another, using only information about the static type of the object being cast

From cppreference.com:

Converts between types using a combination of implicit and user-defined conversions. It has the form static_cast<new_type>(expression).

static_cast is the C++ cast operator used to convert from one type to another. It only uses information about the static type of the object being cast and not its dynamic type; i.e. using only information known at compile-time and not performing a run-time check. The conversion returns a value of type being converted to.

As with all cast expressions, the result is:

  • an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
  • an xvalue if new_type is an rvalue reference to object type;
  • a prvalue otherwise.
507 questions
0
votes
2 answers

Shared_ptr cast vs static_cast speed

Which cast is faster? static_cast of object pointers or static_cast of shared_ptr? Example in qt: class Base; class Derived:Base; auto newBase = QSharedPointer::create(); auto Derived1 = static_cast(newBase.data()); auto Derived2 =…
Ivan
  • 43
  • 4
0
votes
2 answers

Does void* reserve inheritance information?

Is it possible that void* keep type-information? I tried to make it forget the real types (B*) by cast B* b to void* v, but it seems to know its origin. It is good-luck for me, but I don't know why. Here is a simplified code (full demo) :- #include…
javaLover
  • 6,347
  • 2
  • 22
  • 67
0
votes
1 answer

why does this c++ static cast code produce and int not a double

Im new to C++ and i'm not sure why the output for this code is 8 and not 8.25? can someone explain why this code outputs an int not a double? Thanks :) #include "stdafx.h" #include int main() { double x = 8.25; int y; y =…
Kane B
  • 21
  • 2
0
votes
2 answers

CGAL static_cast failing

For my thesis I am using some CGAL code written by another student a year ago. I cannot get it to build, however. The function that is giving errors is the following: std::set originatingCurves(PL_Arrangement arrangement,…
DeMeessias
  • 27
  • 5
0
votes
2 answers

static_cast vs T(n) for fundamental types

Let's assume I have a macro like this: #define IS_SIGNED_B(T) (static_cast(-1)<0) Would it be alright to write it as #define IS_SIGNED_B(T) (T(-1)<0) Knowing that T is (should) always be a fundamental type. And various other cases where I need…
SLC
  • 2,167
  • 2
  • 28
  • 46
0
votes
0 answers

What does "subobject" mean in C++ standard when describing downcast using static_cast

I've read the topic Downcasting a base type and I'm still in doubts. class Base { int i; }; class Derived : public Base { }; int main() { Base* x = new Base(); Derived* y = static_cast(x); return 0; } Do I…
JenyaKh
  • 2,040
  • 17
  • 25
0
votes
1 answer

Why does a const_cast (or static_cast) not add const?

I was looking at this answer and wanted to use. However, I get a segmentation fault, when using the static_cast and const_cast, but if I use a temp variable everything is fine. It is obviously because the non-const version of bar() calls it self…
Jonas
  • 6,915
  • 8
  • 35
  • 53
0
votes
2 answers

Intermittent "Aborted Core Dumped". Maybe static_cast's fault?

I'm writing a graph based backpropagation neural net, as a personal project. Still on the forward prop step. It compiles. Successfully runs half the time, crashes at the very last step half the time. It appears to be dying on some garbage collection…
David Lerner
  • 344
  • 3
  • 14
0
votes
0 answers

Why copy constructor is called?

I have the test code: struct A { A (){} A (const A & a) { std::cout<<"copy A"; } }; int main() { A a; const A& a1 = static_cast(a); return 0; } My question is why copy constructor is called? UPD:…
M90
  • 49
  • 1
  • 6
0
votes
1 answer

C++: downcasting and upcasting between derived template class and base class?

I am having trouble converting a pointer to a templated derived class to a base class pointer for the purposes of storing in a map(and obviously retrieving it later.). I have: #include //Role.h class RoleBase{}; enum class RoleEnum :…
Maths noob
  • 1,684
  • 20
  • 42
0
votes
1 answer

Is static_cast on bounded types implementation-dependent?

I'm looking at static_cast with bounded types . Is the behavior implementation-specific? In other words (given 16-bit shorts and 32-bit longs) is long x = 70000; short y = static_cast(x); guaranteed to produce y = 4464 (the low-order 16 bits…
Charles
  • 479
  • 1
  • 3
  • 13
0
votes
0 answers

Initialize an array of pointers to objects using static downcasting?

My class is asking us to use static-casting in order to downcast our base pointers to derived pointers. class Inventory_Item{ //...functions } class Product : public Inventory_Item { //...functions } class Inventory_System { …
CoffeeAndCream
  • 21
  • 1
  • 1
  • 5
0
votes
1 answer

qt5: connect overload signal and slot function with static_cast

My environment: Qt5.5 + QtCreator3.5 + OSX10.11 I know that the syntax is different on function connect in qt5 and qt4, and look up the Document to figure out how to use connect() in qt5 to deal with overloading signal/slot function. I do as…
Jerry
  • 3
  • 1
0
votes
1 answer

C++ gives me an error: no match for call to

I have a problem using static_cast. Here is my program: #include using namespace std; class Mtx { // base matrix private: // refer to derived class Mtx& ReferToDerived() { return static_cast(*this); } //…
Caboom
  • 11
  • 2
0
votes
0 answers

static_cast parent class to child class C++

Output of this program is "Method B". How can an instance of the parent object call the child class's function through a static_cast? To make things more confusing, if I make method() virtual, then this code outputs "Method A". Can anyone explain…
Dante
  • 139
  • 1
  • 2
  • 6