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

C++ का const_cast

const_cast एक value पर 'do not change' lock remove या add करता है, एक lock pick करने की तरह। इसे कम और carefully इस्तेमाल किया जाना चाहिए।
Syntax
cpp
data_type *pointer_name = const_cast<data_type*>(const_pointer);

data_type &reference_name = const_cast<data_type&>(const_reference);

const_cast क्या है?

const_cast operator एक variable से const qualifier add या remove करने के लिए इस्तेमाल होता है। यह मुख्य रूप से const pointers या references को उन legacy functions को pass करने के लिए इस्तेमाल होता है जो non-const parameters accept करते हैं।

उदाहरण: What is const_cast?

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

void legacyPrint(char *msg) {
	// Print to the console with cout
	std::cout << msg << std::endl;
}

// Program execution starts in main()
int main() {
	const char *text = "Hello";
	legacyPrint(const_cast<char *>(text));
	// Return 0 to signal that the program finished successfully
	return 0;
}

const References Modify करना

अगर एक original variable non-const declare हुआ था, आप इसे एक function को एक const reference की तरह pass कर सकते हैं, और फिर function के अंदर इसकी value temporarily modify करने के लिए const_cast इस्तेमाल कर सकते हैं।

उदाहरण: Modifying const References

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

void modify(const int &x) {
	int &writable = const_cast<int &>(x);
	writable = 100;
}

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

Undefined Behavior Warning

आपको कभी भी एक originally const declared variable modify करने के लिए const_cast इस्तेमाल नहीं करना चाहिए। ऐसा करना Undefined Behavior cause करता है, जो memory corrupt कर सकता है या आपका program crash कर सकता है।

उदाहरण: Undefined Behavior Warning

cpp
#include <iostream>

int main() {
	const int value = 10;
	int &ref = const_cast<int &>(value);
	// ref = 20; would be undefined behavior: value was truly const
	std::cout << "Never modify a truly const original" << std::endl;
	return 0;
}

Overloaded Methods में const_cast

const_cast का एक common use const और non-const objects दोनों के लिए member functions overload करते वक्त code duplication से बचना है। आप const version की logic reuse करके non-const method implement कर सकते हैं।

उदाहरण: const_cast in Overloaded Methods

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

// Define the type Data
class Data {
	// Declare value and set it to 5
	int value = 5;
// Members below can be accessed from outside the class
public:
	const int &get() const { return value; }
	int &get() {
		return const_cast<int &>(static_cast<const Data &>(*this).get());
	}
};

// Program execution starts in main()
int main() {
	Data d;
	d.get() = 42;
	// Print to the console with cout
	std::cout << d.get() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

const_cast Alternatives

const_cast इस्तेमाल करने से पहले, alternative solutions consider करें। अगर आपको एक const function के अंदर एक class member variable modify करना है, उस member variable को इसकी बजाय mutable keyword से declare करें।

उदाहरण: const_cast Alternatives

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

// Define the type Cache
class Cache {
	mutable int hits = 0;
// Members below can be accessed from outside the class
public:
	void access() const { hits++; }
	int getHits() const { return hits; }
};

// Program execution starts in main()
int main() {
	Cache c;
	c.access();
	// Print to the console with cout
	std::cout << c.getHits() << 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. एक originally const declared object modify करने के लिए const_cast इस्तेमाल करना, जो undefined behavior है।
  2. design fix करने की बजाय const errors silence करने के लिए const_cast इस्तेमाल करना।
  3. यह उम्मीद करना कि const_cast const या volatile add या remove करने के अलावा दूसरे types बदलेगा।

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.