← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 11 of 11

C++ का RTTI

RTTI एक program को चलते हुए एक object का real type पता करने देता है, एक mystery box पर label check करने जैसा। typeid और dynamic_cast जैसे tools इसे इस्तेमाल करते हैं।
Syntax
cpp
#include <typeinfo>

typeid(expression).name();
typeid(expression1) == typeid(expression2);

RTTI क्या है?

RTTI, या Run-Time Type Information, वह mechanism है जो C++ program के चलते हुए एक object का exact, actual type determine करने के लिए देता है, सिर्फ compile time पर known static type पर depend करने की बजाय।

उदाहरण: What is RTTI?

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

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual ~Animal() {}
};

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

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Animal *a = new Dog();
	// Print to the console with cout
	std::cout << typeid(*a).name() << std::endl;
	// Free the heap memory with delete
	delete a;
	return 0;
}

typeid Operator

typeid operator एक expression के real runtime type को represent करने वाला एक object return करता है, जिसे आप किसी दूसरे typeid result से compare कर सकते हैं या involved class का एक human-readable (अगर compiler-specific) नाम print करने के लिए इस्तेमाल कर सकते हैं।

उदाहरण: The typeid Operator

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

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Declare y and set it to 3.14
	double y = 3.14;
	// Print to the console with cout
	std::cout << typeid(x).name() << " " << typeid(y).name() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointers पर Dynamic Cast

dynamic_cast operator एक base class pointer को एक derived class pointer में safely convert करता है, runtime पर check करते हुए कि object actually उस derived type का है या नहीं — अगर cast valid न हो, यह एक invalid pointer produce करने की बजाय nullptr return करता है।

उदाहरण: Dynamic Cast on Pointers

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

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual ~Animal() {}
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void bark() { std::cout << "Woof" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Animal *a = new Dog();
	Dog *d = dynamic_cast<Dog *>(a);
	if (d != nullptr) d->bark();
	// Free the heap memory with delete
	delete a;
	return 0;
}

References पर Dynamic Cast

dynamic_cast references पर भी काम करता है, लेकिन क्योंकि एक reference कभी null नहीं हो सकता, एक reference पर एक invalid cast चुपचाप एक null value return करने की बजाय एक std::bad_cast exception throw करता है, जिसे cast के fail होने की संभावना पर आपसे catch करने की उम्मीद है।

उदाहरण: Dynamic Cast on References

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

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual ~Animal() {}
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void bark() { std::cout << "Woof" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog realDog;
	Animal &a = realDog;
	// Try code that might throw an exception
	try {
		Dog &d = dynamic_cast<Dog &>(a);
		d.bark();
	// Handle the exception if one was thrown
	} catch (std::bad_cast &e) {
		std::cout << "Invalid cast" << std::endl;
	}
	return 0;
}

RTTI के साथ Best Practices

RTTI checks की एक छोटी लेकिन real runtime cost होती है, इसलिए जहाँ possible हो अपनी class hierarchy को ordinary virtual function polymorphism के around design करना preferable है, RTTI को उन specific cases के लिए reserve करते हुए — जैसे कुछ plugin या serialization systems — जहाँ आपको genuinely runtime पर एक type inspect करना ज़रूरी हो।

उदाहरण: Best Practices with RTTI

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

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "..." << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Woof" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Animal *a = new Dog();
	a->speak();
	// Free the heap memory with delete
	delete a;
	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. कोई virtual functions न रखने वाली एक class पर dynamic_cast इस्तेमाल करना, जो compile नहीं होता।
  2. pointers पर dynamic_cast का इस्तेमाल करने से पहले एक null result check न करना।
  3. एक pointer पर typeid इस्तेमाल करना और object की बजाय pointer के type की उम्मीद करना।
चैप्टर सारांश
  • Polymorphism function overriding और virtual functions इस्तेमाल करते हुए same call को अलग behave कराता है।
  • Pure virtual functions और abstract classes interfaces define करते हैं जिन्हें derived classes को implement करना ज़रूरी है।
  • Encapsulation, abstraction, object slicing, और RTTI class hierarchies design करने के लिए related concepts हैं।

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.