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
-2
votes
1 answer

c++, static_cast of float point calculation and possible of losing integers

With the following code: int ten{ 1 }; double zeroPnine{ 0.9 }; cout << ten - zeroPnine << endl; // 0.1 cout << (ten - zeroPnine) * 10 << endl; // 1 cout << static_cast (ten - zeroPnine) << endl; // 0 cout <<…
user97662
  • 942
  • 1
  • 10
  • 29
-2
votes
2 answers

c++: Construct derived object via base class interface

I have a template class which is constructed by taking two arguments, an integer and a previous instance of that class. I want to be able to store instances of those classes in containers, which is why I have it inheriting from a base class (please…
linuxfever
  • 3,763
  • 2
  • 19
  • 43
-3
votes
2 answers

C++ static_cast Incorrect

I am new to C++ and have the following simple code snippet exploring C++ limitations: int main() { float x = 2147483000; int y = static_cast(x); cout << "x: " << x << ", y: " << y; } Why is the output showing me different values for x…
juco
  • 57
  • 6
-3
votes
1 answer

static_cast from enum to long yields different output depending on enum members

NOTE1: I have figured the problem before asking it here but I just want a discussion about this non-intuitive behavior in my opinion NOTE2: This happens on gcc 7.5 #include enum lulu { XXX=-2,//if I comment this it considers the…
INS
  • 10,594
  • 7
  • 58
  • 89
-3
votes
1 answer

Use static_cast on non-pointer POD instead of c style cast

is there any reason for using static_cast on non pointer POD data types, like int, float, double? float v = 100; int x = (int) v vs int x = static_castv Is there any reason/advantage on using that, I saw several answers that covers pointers,…
Lefsler
  • 1,738
  • 6
  • 26
  • 46
-3
votes
1 answer

type casting is going somewhere wrong in VC++

I have a class _PDevice which is implemented in PDevice.cpp and declared in PDevice.h Also, in PDevice.h, I have added: typedef QSharedPointer<_PDevice> DDevice; Now, there is another class QLDevice which inherits _PDevice QLDevice also has a…
-4
votes
1 answer

C++ Program, Issue converting integer input to char

I'm currently making an ultra simple tictactoe program but I've run into a slight issue. I've created a gameboard using a 2-D array of characters (not a crazy fan of using chars for this, but figured it was the easiest way to enable me to enter X's…
micronoob
  • 11
  • 2
-4
votes
3 answers

is pointed static_cast valid to avoid copying?

update: class foo { public: foo() : x_(0) { std::cout << "foo constructor\n"; } foo(foo& c) : x_(c.x_) { std::cout << "foo copy- constructor\n"; } foo& operator=(foo const& c) { std::cout << "foo operator=\n"; x_ =…
user1810087
  • 5,146
  • 1
  • 41
  • 76
-5
votes
3 answers

Is reinterpret_cast any slower than a static_cast?

I'm comparing two pointers of class typedef value_type which are each of type T* or char16_t. The compiler complains that I can't compare the two because they are distinct types: 'some_type1::value_type*' and…
Zhro
  • 2,546
  • 2
  • 29
  • 39
-5
votes
2 answers

Does this C++ program invoke undefined behavior?

I was reading about static_cast operator. Consider following example: #include class B { }; class D : public B { public: void fun() { std::cout<<"fun() is called\n"; } }; void f(B* pb,D* pd) { …
Destructor
  • 14,123
  • 11
  • 61
  • 126
-6
votes
2 answers

convert static_castmalloc/free to new/delete

Because segmentation fault related to malloc/free happens, I would like to convert malloc/free to new/delete. Error occurred when malloc/free is converted to below. Let me know how to solve it. (original) char *only_valid_data = static_cast
LenItsuki
  • 57
  • 1
  • 8
-7
votes
2 answers

Cant static cast class to integer

Why am i getting a error when i try to static cast a element* to an int typedef Element* ElementPtr int Element::getVP (ElementPtr item) { return static_cast (item); // I have a class called Element }
Computernerd
  • 7,378
  • 18
  • 66
  • 95
1 2 3
33
34