static_cast
static_cast
is one of the four casting operators in C++. It's used for explicit type conversions that are considered safe by the compiler. This cast is performed at compile-time and is the most commonly used casting operator in C++.
dynamic_cast
)#include <iostream>
int main() {
double pi = 3.14159;
int intPi = static_cast<int>(pi);
std::cout << "Original pi: " << pi << std::endl;
std::cout << "Integer pi: " << intPi << std::endl;
return 0;
}
static_cast
is used to convert a double
to an int
.#include <iostream>
class Base {
public:
virtual void print() { std::cout << "Base" << std::endl; }
virtual ~Base() {}
};
class Derived : public Base {
public:
void print() override { std::cout << "Derived" << std::endl; }
};
int main() {
Base* basePtr = new Derived();
basePtr->print(); // Prints "Derived"
// Downcasting
Derived* derivedPtr = static_cast<Derived*>(basePtr);
derivedPtr->print(); // Prints "Derived"
delete basePtr;
return 0;
}
static_cast
is used for downcasting from Base*
to Derived*
.basePtr
actually points to a Derived
object.static_cast
doesn't perform runtime checks, so it's the programmer's responsibility to ensure the cast is valid.#include <iostream>
#include <cstdlib>
int main() {
// Allocate memory
void* memory = std::malloc(sizeof(int));
// Convert void* to int*
int* intPtr = static_cast<int*>(memory);
// Use the memory
*intPtr = 42;
std::cout << "Value: " << *intPtr << std::endl;
// Free the memory
std::free(memory);
return 0;
}
malloc
returns a void*
, which needs to be cast to a specific type.static_cast
is used to convert void*
to int*
.static_cast
is preferred over C-style cast for this purpose in C++.#include <iostream>
#include <string>
class Person {
public:
explicit Person(const std::string& name) : name_(name) {}
void introduce() const { std::cout << "My name is " << name_ << std::endl; }
private:
std::string name_;
};
int main() {
std::string name = "Alice";
// Using static_cast to call explicit constructor
Person person = static_cast<Person>(name);
person.introduce();
return 0;
}
Person
constructor is marked explicit
, preventing implicit conversion.static_cast
is used to explicitly convert a string
to a Person
object.Safety: While safer than C-style casts, static_cast
still requires caution, especially with pointer conversions.
Performance: static_cast
has no runtime cost as it's resolved at compile-time.
Alternatives: For downcasting, consider dynamic_cast
if runtime type checking is needed.
Readability: Using static_cast
makes the intention of type conversion explicit in the code.
static_cast
is a versatile and type-safe casting operator in C++. It's used for explicit type conversions between related types, including numeric conversions, pointer conversions in class hierarchies, and explicit constructor calls. Unlike C-style casts, static_cast
provides compile-time checking, making it safer and more expressive. However, it doesn't perform runtime checks, so it's crucial to use it correctly, especially when dealing with pointers in inheritance hierarchies. static_cast
is preferred over C-style casts in modern C++ due to its improved type safety and clarity of intent. While powerful, it should be used judiciously, and alternatives like dynamic_cast
should be considered when runtime type checking is necessary.