← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 8 of 17

C++ का static_cast

static_cast एक value को एक type से दूसरी में बदलने का एक careful, clear तरीका है, juice को एक अलग cup में डालने की तरह। यह program build होते वक्त checked होता है।
Syntax
cpp
target_type variable_name = static_cast<target_type>(expression);

static_cast क्या है?

static_cast operator standard, compile-time type conversions के लिए इस्तेमाल होता है। यह एक data type को दूसरे में convert करता है, जैसे float को integer में, और compiler से check होता है यह सुनिश्चित करने के लिए कि conversion safe है।

उदाहरण: What is static_cast?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare f and set it to 3.9f
	float f = 3.9f;
	// Declare i and set it to static_cast<int>(f)
	int i = static_cast<int>(f);
	// Print to the console with cout
	std::cout << i << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Enum Conversions

आप scoped enums (enum classes) को इनकी underlying integer values में convert करने के लिए, या integers को वापस enum values में convert करने के लिए static_cast इस्तेमाल कर सकते हैं।

उदाहरण: Enum Conversions

cpp
// Include std::cout and std::cin
#include <iostream>

enum class Color { Red, Green, Blue };

// Program execution starts in main()
int main() {
	// Declare value and set it to static_cast<int>(Color::Green)
	int value = static_cast<int>(Color::Green);
	// Print to the console with cout
	std::cout << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Inheritance में Upcasting

आप एक derived class pointer को एक base class pointer तक cast करने के लिए static_cast इस्तेमाल कर सकते हैं। इस तरह का conversion हमेशा safe है क्योंकि एक derived object में इसकी base class के सारे components होते हैं।

उदाहरण: Upcasting in Inheritance

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {};
// Define the type Dog
class Dog : public Animal {};

// Program execution starts in main()
int main() {
	Dog d;
	Animal *a = static_cast<Animal *>(&d);
	// Print to the console with cout
	std::cout << "Upcast succeeded" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Void Pointer Conversions

एक void pointer (void*) एक generic pointer है जिसका कोई associated data type नहीं। आप एक void pointer को इसके original typed pointer में वापस convert करने के लिए static_cast इस्तेमाल कर सकते हैं ताकि आप इसे dereference कर सकें।

उदाहरण: Void Pointer Conversions

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 42
	int value = 42;
	void *vptr = &value;
	int *iptr = static_cast<int *>(vptr);
	// Print to the console with cout
	std::cout << *iptr << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

static_cast बनाम C-style Casts

(int)value जैसे older C-style casts के उलट, static_cast ज़्यादा safe है क्योंकि यह compiler से checked होता है। यह आपको accidentally invalid और unsafe type conversions करने से रोकता है।

उदाहरण: static_cast vs C-style Casts

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare d and set it to 3.9
	double d = 3.9;
	// Declare a and set it to (int)d
	int a = (int)d;
	int b = static_cast<int>(d);
	// Print to the console with cout
	std::cout << a << " " << b << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. unrelated types के बीच static_cast इस्तेमाल करना, जो compile नहीं होता।
  2. यह मान लेना कि static_cast runtime पर safety check करता है, जब यह नहीं करता।
  3. गलत derived type में downcast करना, जो undefined behavior है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.