Add content to C++ roadmap
This commit is contained in:
1 parent
056256015d
commit
cc054bb24b
79 files changed
+4073
-80
No files matched your search
@@ -1 +1,70 @@
|
||||
# Scope
|
||||
# Scope in C++
|
||||
|
||||
**Scope** refers to the visibility and accessibility of variables, functions, classes, and other identifiers in a C++ program. It determines the lifetime and extent of these identifiers. In C++, there are four types of scope:
|
||||
|
||||
1. **Global scope:** Identifiers declared outside any function or class have a global scope. They can be accessed from any part of the program (unless hidden by a local identifier with the same name). The lifetime of a global identifier is the entire duration of the program.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
|
||||
int globalVar; // This is a global variable
|
||||
|
||||
int main() {
|
||||
std::cout << "Global variable: " << globalVar << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
2. **Local scope:** Identifiers declared within a function or a block have a local scope. They can be accessed only within the function or the block they were declared in. Their lifetime is limited to the duration of the function/block execution.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
|
||||
void localExample() {
|
||||
int localVar; // This is a local variable
|
||||
localVar = 5;
|
||||
std::cout << "Local variable: " << localVar << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
localExample();
|
||||
// std::cout << localVar << std::endl; //error: ‘localVar’ was not declared in this scope
|
||||
}
|
||||
```
|
||||
|
||||
3. **Namespace scope:** A namespace is a named scope that groups related identifiers together. Identifiers declared within a namespace have the namespace scope. They can be accessed using the namespace name and the scope resolution operator `::`.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
|
||||
namespace MyNamespace {
|
||||
int namespaceVar = 42;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Namespace variable: " << MyNamespace::namespaceVar << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
4. **Class scope:** Identifiers declared within a class have a class scope. They can be accessed using the class name and the scope resolution operator `::` or, for non-static members, an object of the class and the dot `.` or arrow `->` operator.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
|
||||
class MyClass {
|
||||
public:
|
||||
static int staticMember;
|
||||
int nonStaticMember;
|
||||
|
||||
MyClass(int value) : nonStaticMember(value) {}
|
||||
};
|
||||
|
||||
int MyClass::staticMember = 7;
|
||||
|
||||
int main() {
|
||||
MyClass obj(10);
|
||||
std::cout << "Static member: " << MyClass::staticMember << std::endl;
|
||||
std::cout << "Non-static member: " << obj.nonStaticMember << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
Understanding various types of scope in C++ is essential for effective code structuring and management of resources in a codebase.
|
||||
+47
-1
@@ -1 +1,47 @@
|
||||
# Forward declaration
|
||||
# Forward Declaration
|
||||
|
||||
Forward declaration is a way of declaring a symbol (class, function, or variable) before defining it in the code. It helps the compiler understand the type, size, and existence of the symbol. This declaration is particularly useful when we have cyclic dependencies or to reduce compilation time by avoiding unnecessary header inclusions in the source file.
|
||||
|
||||
### Class Forward Declaration
|
||||
|
||||
To use a class type before it is defined, you can declare the class without defining its members, like this:
|
||||
|
||||
```cpp
|
||||
class ClassA; // forward declaration
|
||||
```
|
||||
|
||||
You can then use pointers or references to the class in your code before defining the class itself:
|
||||
|
||||
```cpp
|
||||
void do_something (ClassA& obj);
|
||||
|
||||
class ClassB {
|
||||
public:
|
||||
void another_function(ClassA& obj);
|
||||
};
|
||||
```
|
||||
|
||||
However, if you try to make an object of `ClassA` or call its member functions without defining the class, you will get a compilation error.
|
||||
|
||||
### Function Forward Declaration
|
||||
|
||||
Functions must be declared before using them, and a forward declaration can be used to declare a function without defining it:
|
||||
|
||||
```cpp
|
||||
int add(int a, int b); // forward declaration
|
||||
|
||||
int main() {
|
||||
int result = add(2, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
```
|
||||
|
||||
### Enum and Typedef Forward Declaration
|
||||
|
||||
For `enum` and `typedef`, it is not possible to forward declare because they don't have separate declaration and definition stages.
|
||||
|
||||
Keep in mind that forward declarations should be used cautiously, as they can make the code more difficult to understand.
|
||||
Reference in new issue
Block a user