-
Notifications
You must be signed in to change notification settings - Fork 0
Things to look out for in CPP
In C++, a namespace is a mechanism that allows you to group related declarations, such as functions, classes, and variables, under a specific name. Namespaces help prevent naming conflicts and provide a way to organize code into logical units.
By using namespaces, you can create separate scopes for identifiers, making it possible to have multiple entities with the same name as long as they are in different namespaces.
Here's an example of defining and using a namespace in C++:
#include <iostream>
namespace MyNamespace {
void greet() {
std::cout << "Hello from MyNamespace!" << std::endl;
}
}
int main() {
MyNamespace::greet(); // Accessing the function in the MyNamespace namespace
return 0;
}
In this code, we define a namespace MyNamespace and declare a function greet() inside it. We can then access the function by qualifying it with the namespace name MyNamespace::greet()
.
Namespaces help avoid naming clashes between different libraries, projects, or components by providing a way to encapsulate and separate their declarations. They are especially useful when working with large codebases or collaborating with other developers.
Note: In addition to user-defined namespaces, C++ also has predefined namespaces like std
that contain standard library functions, types, and objects. For example, std::cout
and std::endl
in the above code are part of the std
namespace.
In C++, the friend
keyword is used to declare a non-member function or a non-member class as a friend of a class. When a function or class is declared as a friend, it is granted access to the private and protected members of the class, even though it is not a member of the class itself.
class MyClass {
private:
int privateData;
public:
MyClass() : privateData(0) {}
friend void friendFunction(MyClass& obj);
};
void friendFunction(MyClass& obj) {
obj.privateData = 42; // Accessing the private member of MyClass
}
In this example, the friendFunction
is declared as a friend of the MyClass
class using the friend
keyword. As a result, the friendFunction
can access and modify the private member privateData
of any MyClass
object.
The friend
keyword can also be used to declare an entire class as a friend. This allows the friend class to access the private and protected members of the class declaring it as a friend. For example:
class MyClass {
private:
int privateData;
public:
MyClass() : privateData(0) {}
friend class FriendClass;
};
class FriendClass {
public:
void doSomething(MyClass& obj) {
obj.privateData = 42; // Accessing the private member of MyClass
}
};
In this case, the FriendClass
is declared as a friend of MyClass
. As a result, the FriendClass
can access and modify the private member privateData
of any MyClass
object.
It's important to use the friend
keyword judiciously, as it can introduce tight coupling and reduce encapsulation. It should be used when necessary, such as when implementing operator overloading, providing access for utility functions, or establishing collaborations between classes.