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

C++ के Advanced Interview Questions

ये advanced C++ interviews में पूछे जाने वाले harder questions हैं, smart pointers और templates जैसे topics पर। इन्हें practice करना एक बड़े performance से पहले rehearse करने जैसा है।

Smart Pointers और RAII

Smart pointers dynamic memory पर RAII (Resource Acquisition Is Initialization) apply करते हैं: std::unique_ptr single ownership enforce करता है और scope से बाहर जाने पर automatically अपना object free करता है, जबकि std::shared_ptr reference counting के जरिए कई owners allow करता है -- दोनों एक manual delete याद रखने की ज़रूरत खत्म करते हैं।

उदाहरण: Smart Pointers and RAII

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

// Program execution starts in main()
int main() {
	// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
	std::unique_ptr<int> ptr = std::make_unique<int>(5);
	// Print to the console with cout
	std::cout << *ptr << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Move Semantics और Rvalue References

Move semantics एक object के internal resources -- जैसे एक heap buffer -- को एक temporary से एक नए object में pointer reassignment के जरिए transfer होने देता है, एक पूरी deep copy perform करने की बजाय।

यही चीज़ modern C++ में functions से बड़े objects return करना सस्ता बनाती है।

उदाहरण: Move Semantics and rvalue references

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

// Program execution starts in main()
int main() {
	// Create a vector named source (a resizable array)
	std::vector<int> source = {1, 2, 3};
	// Create a vector named target (a resizable array)
	std::vector<int> target = std::move(source);
	// Print to the console with cout
	std::cout << target.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Virtual Destructors

एक base class जिसे एक base-class pointer के जरिए delete किया जाना है उसे अपना destructor virtual declare करना चाहिए; नहीं तो, एक base pointer के जरिए एक derived object delete करना सिर्फ base class का destructor चलाता है, derived class का cleanup skip करते हुए और इसके owned कोई भी resources leak करते हुए।

उदाहरण: Virtual Destructors

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

// Define the type Base
class Base {
// Members below can be accessed from outside the class
public:
	virtual ~Base() { std::cout << "Base destroyed" << std::endl; }
};

// Define the type Derived
class Derived : public Base {
// Members below can be accessed from outside the class
public:
	~Derived() { std::cout << "Derived destroyed" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Base *b = new Derived();
	// Free the heap memory with delete
	delete b;
	return 0;
}

SFINAE और Concepts

SFINAE ("Substitution Failure Is Not An Error") एक template-metaprogramming technique है जहाँ एक invalid template substitution एक compile error cause करने की बजाय silently overload resolution से discard हो जाता है, templates को यह constrain करने देते हुए कि वे कौन-से types accept करते हैं।

C++20 Concepts इसे readable, explicit syntax में formalize करते हैं।

उदाहरण: SFINAE and Concepts

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

// Declare a template so this works with any suitable type
template <typename T>
typename std::enable_if<std::is_integral<T>::value, void>::type
show(T value) {
	// Print to the console with cout
	std::cout << "Integral: " << value << std::endl;
}

// Program execution starts in main()
int main() {
	show(5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Perfect Forwarding और std::forward

Perfect forwarding, universal references (T&&) लेने वाले एक template function के अंदर std::forward इस्तेमाल करते हुए, यह preserve करता है कि हर argument originally एक lvalue था या rvalue जब यह किसी दूसरे function को pass होता है -- generic wrapper functions के लिए essential जिन्हें accidentally एक unnecessary copy force नहीं करना चाहिए।

उदाहरण: Perfect Forwarding and std::forward

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

void process(int &x) { std::cout << "lvalue" << std::endl; }
void process(int &&x) { std::cout << "rvalue" << std::endl; }

// Declare a template so this works with any suitable type
template <typename T>
void wrapper(T &&arg) {
	process(std::forward<T>(arg));
}

// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	wrapper(a);
	wrapper(10);
	// 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. यह मान लेना कि shared_ptr और unique_ptr ownership interchangeable हैं, जब unique_ptr सिर्फ एक owner allow करता है।
  2. इस पर std::move call करने के बाद एक object इस्तेमाल करना।
  3. polymorphically इस्तेमाल होने वाली एक base class में एक virtual destructor भूल जाना।
चैप्टर सारांश
  • Namespaces, header files, और multi-file programming बड़े projects organize करते हैं।
  • चार cast operators, static_cast, dynamic_cast, const_cast, और reinterpret_cast, अलग-अलग तरीकों से types convert करते हैं।
  • Threads, mutexes और locks, async और future, debugging, input validation, mini projects, और interview questions C++ को real work में apply करते हैं।

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.