Namespaces और C Name Prefixes
C++ namespace scope बनाता है; C libraries public identifiers prefix करती हैं।
Namespaces और C Name Prefixes क्या है?
C++ namespace scope बनाता है; C libraries public identifiers prefix करती हैं।
बड़े programs में symbol collision रोकें।
महत्वपूर्ण बातें
- पहले C mechanism समझें।
- Ownership के लिए RAII और value semantics उपयोग करें।
- Macros और casts से अधिक type-safe abstractions चुनें।
C और C++ code examples
main.c
#include <stdio.h>
int math_square(int value) {
return value * value;
}
int main(void) {
printf("%d\n", math_square(7));
}
49
C++
main.cpp
#include <iostream>
namespace math {
int square(int value) {
return value * value;
}
}
int main() {
std::cout << math::square(7) << '\n';
}
49
C और C++ की तुलना
C prefix convention है; C++ namespace compiler लागू करता और alias तथा qualified lookup देता है।
C
C prefixes, callbacks, macros और context structs उपयोग करता है।
C++
Language scoped और type-safe abstractions देता है।
अभ्यास
दोनों versions चलाकर बदलें और language guarantees की तुलना करें।
- पहले C mechanism लिखें।
- Manual cleanup को RAII से बदलें।
- Allocation और virtual dispatch जाँचें।