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

callback functions and static_cast for wrapping class methods

I'm having some trouble making a callback wrapper class method that needs to be used by a third party library; the JackAudio library. I have been able to make a wrapper for a JackAudio callback function that needs two arguments. I'm just having…
Tek
  • 2,888
  • 5
  • 45
  • 73
0
votes
3 answers

c++ dangerous casting code

I'm pretty sure this is dangerous code. However, I wanted to check to see if anyone had an idea of what exactly would go wrong. Suppose I have this class structure: class A { protected: int a; public: A() { a = 0; } int getA() { return…
Abe Schneider
  • 977
  • 1
  • 11
  • 23
0
votes
1 answer

Why can't I use a static_cast from one Xalan class to its base class?

Why does the compiler (g++) complain about this line of code? XalanNode *docElement = static_cast (docBuilder_->getDocument()->getDocumentElement()); The error I get from the compiler is: error: invalid static_cast from type…
snibbets
  • 1,095
  • 10
  • 11
0
votes
1 answer

Regular cast doesnot throw runtime error

If we see the below code, fun function converts C's object into B's object and calls B' own function. How doesn't it give segm fault. I think this will lead to crash. My program is not crashed. Can any one explains why is it working…
ranganath111
  • 457
  • 2
  • 7
  • 17
0
votes
1 answer

reinterpret_cast and null member variables

I'm using reinterpret_cast something like this: void RunThread (void *myself) { (reinterpret_cast(myself))->Method(); } Inside Method, most of my member variables (all Handles) are null. Could this be because of reinterpret_cast since…
Science_Fiction
  • 3,403
  • 23
  • 27
0
votes
3 answers

Is downcasting this during construction safe?

I have a class hierarchy where I know that a given class (B) will always be derived into a second one (D). In B's constructor, is it safe to statically cast the this pointer into a D* if I'm sure that nobody will ever try to use it before the entire…
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
0
votes
1 answer

static_cast in default argument value

I would like to have a constructor with default argument static_cast like: generate_word_spot(func_double_double_t& cost_f = static_cast(func_const_t(1))) : cost_f_(cost_f) {}; where class func_const_t : public…
Emre Sahin
  • 468
  • 4
  • 14
0
votes
1 answer

Getting First digit in double and storing it in an int C++

Hello im coding in C++ and i need some help with converting a double to an int. what a need is a way to get the first number from a double ie (3.5945) "3". and put that number into an int. I'm using static_cast now and its returning a 0. double X =…
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52
-1
votes
1 answer

How can I make static_cast from unique_ptr

I have the following code, how to cast correctly from unique_ptr to base class?: class MagEventNotifierAndSupplier : public CSubject, IMagneticData { public: //implement } unique_ptr
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
-1
votes
1 answer

Implicit conversion in c++ for between unsigned types of bit length ie uint8_t,uint16_t

I would like to know how to implicit casting works in case of expressions between unsigned int of various bits ie uint8_t,uint16_t etc and ways to avoid it explicitly. For this reason i sumarrized the following cases: How implicit casting in case…
-1
votes
1 answer

Lambda expression in C++17: trailing return type vs static_cast for type conversion

How to convert int to long inside/outside of lambda expression properly? How to check overflow of mathematics inside lambda properly? int n = 12; // input parameter from std::cin int a = 23; // input parameter from std::cin int i = 34; // input…
-1
votes
1 answer

Casting and freeing between multiple levels of inheritance c++

Does order matter when casting and does this have an effect on freeing an object with a virtual base class? E.g. if you upcasted 2 levels, must you downcast 2 levels, or can you downcast 1 level at a time? struct A { virtual ~A() = default; …
DaE-Asew
  • 137
  • 1
  • 7
-1
votes
2 answers

how to refer to a base class's derived class?

class Element { }; class Container { vector elements; }; The above is the original code. I was told not to change the above code. Now I have class IndexElement: public Element { int b_index; }; class Container* ctr; Now I have…
Taitai
  • 584
  • 2
  • 7
  • 18
-1
votes
2 answers

static_cast of unique_ptr to void *, with constructor arguments

I have a problem in making a static_cast of an unique_ptr void * by passing arguments to the constructor. In particular, what you see in the object3. It is important that the solution is at compile time, and the type must be the same for smart…
Nadalet
  • 57
  • 1
  • 7
-1
votes
1 answer

Clang fails to detect uninitialized class members in assignment operator/ copy constructor

Clang doesn't check if all class members have been initialized inside overloaded assignment operators/copy constructors in the contrary to Lint. Instead of that Clang check usage of uninitialized variables. Such approach should be sufficient but…