Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Foundation  ·  Lesson 10

Type Conversion and Type Casting

Type casting क्या है?

Type casting किसी value को एक type से दूसरे में बदलता है — double को int, base pointer को derived में। C++ चार नामित cast operators देता है जो हर conversion को स्पष्ट और पुराने C-style (type)value रूप से सुरक्षित बनाते हैं।

C-style casts

पुराना तरीका अब भी compile होता है पर इरादा छिपाता है — यह चुपचाप कई conversions आज़माता है:

C++
double d = 3.9;
int x = (int)d;      // C-style: chalta hai, par yeh kaun-sa cast hai?

नीचे दिए नामित casts पसंद करें — वे साफ़ और खोजने योग्य हैं।

static_cast

रोज़मर्रा का cast: numeric conversions और upcasts, compile time पर जाँचे:

C++
double d = 3.9;
int x = static_cast<int>(d);     // 3 (truncate)
cout << x << "\n";
Output:
3

dynamic_cast

Polymorphic hierarchy में सुरक्षित downcasting को — run time पर जाँचा, विफलता पर null लौटाता:

C++
class Animal { public: virtual ~Animal() {} };
class Dog : public Animal { public: void bark() {} };

Animal *a = new Dog();
Dog *d = dynamic_cast<Dog*>(a);   // safal → null nahi
if (d) d->bark();

काम करने को इसे base में एक virtual function चाहिए।

const_cast

const जोड़ता या हटाता है — मुख्यतः पुराने non-const-correct APIs call करने को:

C++
void oldApi(char *s);            // const-correct nahi
const char *msg = "hi";
oldApi(const_cast<char*>(msg));  // sirf tab safe jab oldApi badle nahi

reinterpret_cast

सबसे खतरनाक — raw bits को असंबंधित type के रूप में फिर व्याख्या करता है। केवल low-level काम को:

C++
int n = 65;
int *p = &n;
long addr = reinterpret_cast<long>(p);   // pointer → integer

कौन-सा cast इस्तेमाल करें

Castकिसके लिएजाँच
static_castNumeric, upcastsCompile time
dynamic_castसुरक्षित downcastsRun time
const_castconst जोड़ना/हटानाCompile time
reinterpret_castLow-level bit फिर-व्याख्याकोई नहीं

आम गलतियाँ

  • जहाँ static_cast सही है वहाँ reinterpret_cast इस्तेमाल करना।
  • const_cast के बाद सचमुच const object बदलना (undefined behaviour)।
  • Pointer इस्तेमाल करने से पहले dynamic_cast को null जाँचना भूलना।
  • ऐसे C-style casts पर निर्भर रहना जो खतरनाक conversion छिपाएँ।
🏋️ अभ्यास

एक Animal base बनाएँ जिसमें Dog और Cat subclasses हों। इन्हें Animal* के रूप में रखें, फिर dynamic_cast से पता करें कौन-से dogs हैं और dog-only method सुरक्षित रूप से call करें।

सारांश

  • Type casting types के बीच बदलता है; C++ नामित casts पसंद करें।
  • सामान्य compile-time conversions को static_cast।
  • सुरक्षित run-time downcasting को dynamic_cast (virtual चाहिए)।
  • const_cast const जोड़ता/हटाता; reinterpret_cast bits फिर-व्याख्या करता।
  • नामित casts C-style casts से साफ़ और सुरक्षित हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में type casting क्या है?
Type casting किसी value को एक type से दूसरे में बदलना है, जैसे double को int में या base pointer को derived pointer में बदलना। C++ चार नामित cast operators देता है — static_cast, dynamic_cast, const_cast और reinterpret_cast — जो हर conversion का इरादा स्पष्ट और पुराने C-style casts से सुरक्षित बनाते हैं।
C++ में static_cast और dynamic_cast में क्या अंतर है?
static_cast compile time पर जाँचे conversions करता है, जैसे numeric conversions या upcasts, बिना run-time लागत। dynamic_cast inheritance hierarchy में सुरक्षित downcasting को इस्तेमाल होता है और run time पर जाँचता है कि object सचमुच target type का है या नहीं, न होने पर null (pointers के लिए) लौटाते हुए।
C++ में const_cast कब इस्तेमाल करना चाहिए?
const_cast किसी type से const (या volatile) जोड़ता या हटाता है। इसका मुख्य वैध इस्तेमाल किसी पुराने API को call करना है जो const-correct नहीं, ऐसे data के साथ जिसे बदलना आप सुरक्षित जानते हों; const हटाकर फिर सचमुच const object बदलना undefined behaviour है।
C++ में reinterpret_cast क्या करता है?
reinterpret_cast किसी value के bit pattern को एक अलग, असंबंधित type के रूप में फिर से व्याख्या करता है, जैसे pointer को integer में बदलना। यह सबसे खतरनाक cast है, low-level काम को इस्तेमाल होता, और portability की कोई गारंटी नहीं देता, तो जब तक बिल्कुल ज़रूरी न हो इससे बचना चाहिए।
C-style casts के बजाय C++ नामित casts क्यों पसंद करें?
नामित casts आपका इरादा स्पष्ट बनाते हैं और compiler को सीमाएँ लागू करने देते हैं — static_cast चुपचाप const नहीं हटाएगा, उदाहरण के लिए। C-style cast बारी-बारी कई conversions आज़माता है और खतरनाक वालों को छिपा सकता है, जबकि नामित casts खोजने योग्य, पाठकों को साफ़, और सुरक्षित हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicConstants and Literals in C++ आपकी प्रगति: 10 / 81 (12%) · Lesson 11 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें