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
0 answers

Why static_cast doesn't cast to the class which has been initialized

I want to cast the data to class instance using static_cast. Strangely it always cast's to base class and not the sub class which is initialized. Here is the example code: #include using namespace std; class A { public: void Display…
Royal Pinto
  • 2,869
  • 8
  • 27
  • 39
0
votes
1 answer

Losing precision in stringstream

In one of my applications I am trying to put a float value into a string stream like this: stream << static_cast(double value); Instead of getting the entire float value I get only the integer part of it. Any idea why that might happen?
0
votes
1 answer

Using static_cast and then dynamic_cast

I'm dealing with a special case where I can't use dynamic_cast directly because the object is a void*. Is using first static_cast on it and then dynamic_cast (on the result of the static_cast) bad practice? Is it wrong? Here's an example of what I'm…
Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
0
votes
1 answer

Cast from pointer to reference

I have a generic Singleton class that I'll use for many singleton classes. The generic class is giving a compile error when converting from a pointer to reference. Error 3 error C2440: 'static_cast' : cannot convert from 'IApp *' to 'IApp…
sazr
  • 24,984
  • 66
  • 194
  • 362
0
votes
0 answers

Is it safe to replace dynamic_cast with static_cast?

For a data analysis program I am using a library which solely uses dynamic_cast instead of static_cast. After profiling and optimizing my own code, valgrind shows me that my program is spending roughly 50% of the time doing dynamic_cast in library…
Jan Hajer
  • 232
  • 2
  • 14
0
votes
0 answers

Issue With Getting the Correct Data (doubles and ints)- C++

So the issue that I'm running into with this piece of code is that if the repetitions exceed 6 then the program crashes. Playing around with it I realized first of all the sums weren't showing up correctly with some couts I placed to test, changing…
Moxy
  • 171
  • 1
  • 3
  • 12
0
votes
1 answer

implement static cast on C++

I am trying to implement static cast. I need to check if the types T and U are implicitly convertible, if not check if one inherites from another. I can write a class to check each on of them, but I cant understand how to check implicit convert and…
shay
  • 1,211
  • 2
  • 8
  • 6
0
votes
2 answers

cast pointer to pointer as LPVOID*

I have following code: IShellLink* psl; HRESULT hres = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); It is correctly compiled. But I need to replace (LPVOID*)&psl by *_cast.…
vladon
  • 8,158
  • 2
  • 47
  • 91
0
votes
2 answers

Why is assignment slower when there's an implicit conversion?

If there was similar questions please direct me there, I searched quiet some time but didn't find anything. Backround: I was just playing around and found some behavior I can't completely explain... For primitive types, it looks like when there's an…
Ooops
  • 269
  • 2
  • 12
0
votes
1 answer

Static cast of enum to bool, performance warning from Compiler

I have the following declared in my project: enum class OType : bool { Dynamic=true, Static=false }; OType getotype(); I'm using the following function: double ComputeO(double K,bool type) I'm calling it this way : ComputeO(some double,…
user2164703
  • 199
  • 1
  • 11
0
votes
2 answers

Appending "pointer to a child class" into a vector of "pointer to the parent class"

I am having some difficulties with static casting my child classes into the parent classes using the insert function for "std::vector::insert" Here is some code to sense of what Im trying to do: mInstancedObjects.insert(mInstancedObjects.end(),…
0
votes
1 answer

Reading in from a .txt file to a struct array that contains enum

Here is my code enum Status {IN, OUT }; const int TITLE_SIZE = 50, ISBN_SIZE = 13, AUTHOR_SIZE = 25; struct Info { char title[TITLE_SIZE]; char isbn[ISBN_SIZE]; char author[AUTHOR_SIZE]; Status inOrOut; }; int main() { fstream…
0
votes
3 answers

How to type cast int division to floating point?

#include #include using namespace std; int main() { float f=static_cast(5/2); printf("%f",f); return 0; } The answer is always 2.0. I searched before asking but couldn't find the answer.
Naresh
  • 334
  • 1
  • 4
  • 17
0
votes
2 answers

Determining the number of decimal digits in a double - C++

I am trying to get the number of digits after a decimal point in a double. Currently, my code looks like this: int num_of_decimal_digits = 0; while (someDouble - someInt != 0) { someDouble = someDouble*10; someInt = someDouble; …
0
votes
2 answers

static_cast in a for loop

I know that static casts are handled at compile time, but what about a static cast in the conditional of a for loop? Will the cast be made with each iteration, or only once during compilation? I'm concerned about the cost of this implementation. I…