Questions tagged [reinterpret-cast]

A C++ operator that simply allows the conversion between types by reinterpreting the underlying bit pattern. In general use, this amounts a pointer to be converted into any other pointer type and it can also allow an integral type to be converted into any pointer type and vice versa.

A reinterpret_cast directs the compiler to "view" or treat the memory as if it were the new type (being cast to).

From cppreference.com:

Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.

There are limitations on what reinterpret_cast can do whilst remaining valid, in particular type aliasing can become a problem.

581 questions
-3
votes
2 answers

Hacking private data using c++

Clarification: This question originally came from a challenge I thought of, and isn't connect with programming for real systems. Suppose I have a class, that I know its' architecture which I can't change, and I don't want to inherit it, but I do…
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
-3
votes
1 answer

Downcast object which was instantiated with parent class

I have a parent-child inheritance structure. I have an object that was instantiated(new) by parent class. I want to downcast this object to child class. I need a automatic routine like casting because parent class has a lot of properties and copying…
hamed
  • 471
  • 1
  • 9
  • 22
-3
votes
1 answer

What is meant by the highlighted lines in the below program?

The code here is being used for creating a Student Report card project but while trying to understand we can not figure out the use of and functions of the lines below from the code. This: File.read(reinterpret_cast (&st),…
-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…
-3
votes
1 answer

Why does fs << reinterpret_cast(&something) produce unreadabble output?

everyone. I am having a hard time understanding what reinterpret_cast is doing. I have this piece of code: unsigned int no = 10; ofstream fs = ofstream("output.out", ios_base::out); if (!fs.bad()) { …
MaryOz
  • 9
-4
votes
2 answers

Is there a way to reinterpret_cast to a virtual derived* and calling overriden from parent?

#include template struct printer { virtual const T* get(size_t& sz) const = 0; void print() { size_t sz; const T* _t = get(sz); //tries to access 'this' for (size_t t = 0; t < sz; t++) …
PinkTurtle
  • 6,942
  • 3
  • 25
  • 44
-4
votes
3 answers

convert char* to structure in c++

I have the following structure struct record{ RType m_rectype; char m_recordname[11]; char m_recordNo; char m_record_date[6]; } and I have a following line of char* type line = "1Netherlands3240382" How can I covert this line into the structure.…
Fawad
  • 23
  • 7
-4
votes
2 answers

reinterpret_cast, casting to brother class

I am just wondering if the following C++ code guaranteed to work: struct B1 { virtual void f() {}; }; struct B2 { virtual void f2() {}; }; struct D:public B1,public B2 { }; int main() { D d; B1 *b1=&d; if…
gena2x
  • 365
  • 2
  • 12
-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
1 answer

Concept of reinterpret cast and static cast ?

I can't understand the concept of reinterpret_cast and static_ cast. Can anyone please explain in easy method using ASCII Art or something like that..
Kashiii
  • 37
  • 2
  • 3
  • 13
-10
votes
2 answers

How is std::vector getting initialized in this case?

I recently came across a snippet of code which stroke me as odd. #include #include #include int main() { std::vector> idx; for (auto ii = 0; ii < 2 * 10; ii += 2) { …
user14089818
1 2 3
38
39