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

C++ static_cast and virtual method functionality

if ((char*)fixtureAData == "PLATFORM" && (char*)fixtureBData == "WEAPON"){ static_cast(contact->GetFixtureA()->GetBody()->GetUserData())->SetLethality(false); This is a snippet of code from a team project I'm currently working on. I…
eszeikial
  • 98
  • 8
0
votes
2 answers

Malloc Memset is this usage right ?

Whats wrong with this usage void* buffer ; buffer = (void *)malloc(4096) ; memset( buffer, 0, sizeof(buffer) ); int *data = static_cast(buffer) ; for(int i=0 ; i<10 ; i++) { cout<< data[i] << "\n" ; } I get garbage values in the…
akslah
  • 49
  • 7
0
votes
3 answers

Understanding static_cast

I have some problem with understanding the code below. Why am I able to call c() function? #include #include using namespace std; class A {}; class B : public A { public: void c() { cout << "c: B" << ++x << endl; } int…
szym4n
  • 25
  • 1
  • 4
0
votes
1 answer

function that receives a letter then returns the next and previous letters c++

Write a function that receives a letter then returns the next and previous letters. and this is my Solution #include using namespace std; void letter(char &x); int main(){ char a; cout<<"Enter your letter\n:"; cin>>a; …
0
votes
3 answers

Identifying derived class type from a base class

In this scenario where classes DeriA and DeriB inherit from Base: class Base class DeriA : public Base class DeriB : public Base std::list objects; Is it possible to check what type of class is being inherited from each member of the objects…
Xemerau
  • 13
  • 3
0
votes
3 answers

Converting time_t to an int

I want to convert the unix timestamp returned by time() as time_t to an integer. I've been searching for a solution for 20 minutes, and decided to ask here. Every solution I have found has not worked. When trying to cast from time_t to int, I get…
Michael
  • 23
  • 1
  • 5
0
votes
3 answers

c++: how can I avoid the static_cast?

I have an abstract base class and a templated derived class. Derived objects can be constructed by a previous instance of a derived object and, say, an integer. So far we have struct base { /* ...pure virtual functions here... */ virtual…
linuxfever
  • 3,763
  • 2
  • 19
  • 43
0
votes
3 answers

static_cast from char to enum assigning wrong value

I can't figure out why my code is returning the wrong value. input of 'a' returns 97 and 'z'returns 122. What am i doing missing? int main() { enum Alphabet {a = 1, b = 2, c = 3,d = 4,e = 5,f = 6,g = 7,h = 8,i = 9,j = 10,k = 11,l = 12,m =…
user1152145
  • 275
  • 1
  • 3
  • 11
0
votes
2 answers

Is this static_cast valid/legit?

double diff = static_cast(a- b); a and b are of type int64_t. I saw this code in our project. I think it is suspicious, but I am really not sure. I am familiar with static_cast, and I would not write code like this. Is this static_cast…
BufBills
  • 8,005
  • 12
  • 48
  • 90
0
votes
1 answer

static_cast literal 0 to other types in STL

Which type can 0 be cast to? pointers, numeric vars. Any others? Will the following cast be safe? ps: an excerpt from STL implementation of iterator template inline typename…
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
0
votes
2 answers

Correct c++-style cast for fixed size arrays?

I have a small question regarding casts. Basically, I have the following code : //Array of ALbyte, size is irrelevant ALbyte buffer[1000]; //... alcCaptureSamples(m_audioDevice,(ALCvoid*)buffer, sample); and based on Scott Meyers Effective C++…
JBL
  • 12,588
  • 4
  • 53
  • 84
0
votes
1 answer

static_cast's argument forwarding

Suppose i don't like the name of static_cast operator and want to wrap it in a function with a different name, say fancy_static_cast but perfectly preserving the semantics. How should i do it? More specifically does static_cast accept it's argument…
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
0
votes
1 answer

invalid static_cast from type 'QCell*' to type 'QWidget*'

this error appears in line 4: void QPiece::setPosition( QPoint value ) { _position = value; QWidget* parentWidget = static_cast( _board->Cells[value.x() ][ value.y() ]); if (parentWidget->layout()) { …
Robert
  • 371
  • 1
  • 5
  • 15
0
votes
1 answer

using dynamic_cast for runtime type identification

when reading Essential c++ chapter 5.10 Run-time Type identification, I've encountered a problem. Let me introduce a little background first. There are a base class named num_sequence and a class Fibonacci derive from num_sequence. In the base…
Fihop
  • 3,127
  • 9
  • 42
  • 65
0
votes
3 answers

C++ compilation error in static_cast< >

Below is my code Class A { A::A(int num) { } int num; }; class B : public A { B::B(int num):A(num) { } }; Class D; Class C { void getNum(A**& somenum) {} D *dObj; }; void C::getNum(A**& somenum) { …
Jabez
  • 1,883
  • 6
  • 19
  • 18