C++ का RTTI
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
typeid Operator
typeid operator एक expression के real runtime type को represent करने वाला एक object return करता है, जिसे आप किसी दूसरे typeid result से compare कर सकते हैं या involved class का एक human-readable (अगर compiler-specific) नाम print करने के लिए इस्तेमाल कर सकते हैं।
उदाहरण: The typeid Operator
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- कोई virtual functions न रखने वाली एक class पर
dynamic_castइस्तेमाल करना, जो compile नहीं होता। - pointers पर
dynamic_castका इस्तेमाल करने से पहले एक null result check न करना। - एक 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 हैं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: