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
-1
votes
3 answers

What does reinterpret_cast(&st) and (-1)*static_cast mean?

The code here is being used for creating a Student Report card project. While trying to understand we can not figure out the use of and functions of the below code: File.read(reinterpret_cast (&st), sizeof(student)); int…
Amrita CSE
  • 21
  • 1
  • 2
  • 3
-1
votes
2 answers

Why the constructor is called after a static cast?

This is my class: class AComponent : public nts::IComponent { public: AComponent(const size_t &maxInputs, const size_t &maxOutputs, const size_t &value); AComponent(nts::AComponent &); virtual ~AComponent(); virtual nts::Tristate…
Dimitri Danilov
  • 1,338
  • 1
  • 17
  • 45
-1
votes
1 answer

Implementation of static_cast operator and it's limits

Could please tell why compiler is not allowing this type cast...Error compiler showing is " Invalid static_cast from float * to int * " #include using namespace std; int main() { float f=45.678; float *a; a=&f; int *d; …
virusai
  • 27
  • 1
  • 7
-1
votes
2 answers

A Floating point expression whose value is 1 shows 0 when converted to int. How to get rid of this isuue in C++?

I need to change an expression's type from floating point to integer so as to add it to another integer variable. The actual value of the expression is 1. But the expression value ((d*s)/sqrt(1+pow(s, 2))) shows 0 when changed from floating point to…
Moriarity
  • 25
  • 6
-1
votes
2 answers

Static cast - Cannot cast through virtual inheritance

I was reading about static and dynamic casts along with the differences between them. It states that static_cast cannot cast through virtual inheritance however dynamic cast can. I would appreciate it if someone could clarify what this means…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
-1
votes
1 answer

c++ static and dynamic type casting

Why static_cast<>() (also known as downcast) doesn't casts types run-time while dynamic_cast<>() (also known as upcast) does? Both are used to advance through class hierarchy. What could be so different between them that it is necessary for…
Ivars
  • 2,375
  • 7
  • 22
  • 31
-1
votes
3 answers

Which is the idiomatic way to document a static_cast?

I understand that (MyType)foo, MyType(foo), and static_cast(foo) are somewhat similar in that the first two become the last (EDIT: I have been corrected. The previous sentence is untrue.) My question is which is the idiomatic version to use?…
griotspeak
  • 13,022
  • 13
  • 43
  • 54
-1
votes
1 answer

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

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…
lurscher
  • 25,930
  • 29
  • 122
  • 185
-2
votes
1 answer

Memory issue in casting from a Derived class to a Base class

I have a class Base and another class Derived (derived from Base). Also I have a 2 functions GetValue(Base*) and GetDerivedValue(Derived*). Now, from the main() function, I have access to only GetValue(), but I need the value of both Base and…
-2
votes
1 answer

static_cast of std::initializer_list::size_type spits out "invalid conversion" error

I'm working through "A Tour of C++" by BS, and am recreating the Vector class that he uses throughout, at least the first four chapters. I hit a snag building the second version of the Vector constructor with a std::initializer_list. A…
JJB_UT
  • 3
  • 2
-2
votes
1 answer

Use static_cast to dynamic polymorphism

I used a static_cast to polymorphism. I read that I should use dynamic_cast, but I can't get why. Code works in both way (with static and dynamic cast). Could anybody show me an example with necessary dynamic_cast operator? class Base { …
KeyB0rys
  • 392
  • 5
  • 13
-2
votes
1 answer

static_cast doesn't work. C style cast works. How to repair static_cast?

It works fine: srand(time(NULL)); cout<<(double)rand()/RAND_MAX<(rand()/RAND_MAX)<
-2
votes
1 answer

Using lots of static casting for my array of base class objects - is this ok?

Is a lot of static casting a bad idea ? Is there a way to avoid static casting when using containers whose element type is a base class of some subclass hierarchy? Example: The case of using std::vector to store game objects. These…
-2
votes
1 answer

what is wrong this static_cast piece of code?

I have this piece of code in c++ char a; cin>>a; //I input 3 in this a=static_cast(a); cout<(4.2) cout<
CuriousAlpaca
  • 131
  • 1
  • 7
-2
votes
1 answer

How to use static_cast in this statement

I have the statement: amount *= factor; amount is defined as an integer, where as factor is defined as double. So the result of multipling amount and factor would be of type double. I would like to use static_cast to make sure the resulting amount…
David
  • 619
  • 2
  • 8
  • 15
1 2 3
33
34