-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New entry mutable variables #5555
base: main
Are you sure you want to change the base?
Conversation
This reverts commit 42f045e.
Hey @codecademy-docs please review |
Hey @Sriparno08 please review |
Hey @braanj, I'll review this by the end of the week. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @braanj, thank you for contributing to Codecademy Docs!
I've listed all the modifications that you need to make in your entry below:
--- | ||
Title: 'Mutable variables' | ||
Description: 'In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly.' | ||
Subjects: | ||
- 'Computer Science' | ||
- 'Game Development' | ||
Tags: | ||
- 'Variables' | ||
CatalogContent: | ||
- 'learn-c-plus-plus' | ||
- 'paths/computer-science' | ||
--- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modifications:
- Let's write the title in title case
- Let's change the description a bit
- Let's add some more tags
--- | |
Title: 'Mutable variables' | |
Description: 'In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly.' | |
Subjects: | |
- 'Computer Science' | |
- 'Game Development' | |
Tags: | |
- 'Variables' | |
CatalogContent: | |
- 'learn-c-plus-plus' | |
- 'paths/computer-science' | |
--- | |
--- | |
Title: 'Mutable Variables' | |
Description: 'Mutable variables are variables that can be modified even within constant functions.' | |
Subjects: | |
- 'Computer Science' | |
- 'Game Development' | |
Tags: | |
- 'Functions' | |
- 'Values' | |
- 'Variable Types' | |
- 'Variables' | |
CatalogContent: | |
- 'learn-c-plus-plus' | |
- 'paths/computer-science' | |
--- |
- 'paths/computer-science' | ||
--- | ||
|
||
In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make some minor changes:
In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly. | |
In C++, mutable variables are variables that can be modified even within constant functions, useful for managing internal state flexibly. |
|
||
## Syntax | ||
|
||
To declare a variable as `mutable`, simply place the `mutable` keyword before the variable type: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make some minor changes:
To declare a variable as `mutable`, simply place the `mutable` keyword before the variable type: | |
To declare a variable as mutable, the `mutable` keyword needs to be placed before the variable type: |
- **type** - The type of the variable (e.g., `int`, `char`). | ||
- **name** - The name of the variable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make some minor changes:
- **type** - The type of the variable (e.g., `int`, `char`). | |
- **name** - The name of the variable. | |
- `type`: The type of the variable (e.g., `int`, `char`). | |
- `name`: The name of the variable. |
|
||
## Example | ||
|
||
In the example below, `accessCount` is marked `mutable`, allowing it to be modified within the const `displayData` function: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make some minor changes:
In the example below, `accessCount` is marked `mutable`, allowing it to be modified within the const `displayData` function: | |
In the example below, `accessCount` is marked `mutable`, allowing it to be modified within the constant `displayData()` function: |
```cpp | ||
#include <iostream> | ||
#include <string> | ||
|
||
class Data { | ||
public: | ||
Data(std::string value) : data(value), accessCount(0) {} | ||
|
||
void displayData() const { | ||
++accessCount; // Allowed modification due to `mutable` | ||
std::cout << "Data: " << data << ", Access count: " << accessCount << std::endl; | ||
} | ||
|
||
private: | ||
std::string data; | ||
mutable int accessCount; // Can be modified in const methods | ||
}; | ||
|
||
int main() { | ||
Data d("Sample"); | ||
d.displayData(); | ||
d.displayData(); | ||
return 0; | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change the indentation to 2 spaces and make some minor changes:
```cpp | |
#include <iostream> | |
#include <string> | |
class Data { | |
public: | |
Data(std::string value) : data(value), accessCount(0) {} | |
void displayData() const { | |
++accessCount; // Allowed modification due to `mutable` | |
std::cout << "Data: " << data << ", Access count: " << accessCount << std::endl; | |
} | |
private: | |
std::string data; | |
mutable int accessCount; // Can be modified in const methods | |
}; | |
int main() { | |
Data d("Sample"); | |
d.displayData(); | |
d.displayData(); | |
return 0; | |
} | |
``` | |
```cpp | |
#include <iostream> | |
#include <string> | |
class Data { | |
public: | |
Data(std::string value) : data(value), accessCount(0) {} | |
void displayData() const { | |
++accessCount; // Modification allowed due to 'mutable' | |
std::cout << "Data: " << data << ", Access count: " << accessCount << std::endl; | |
} | |
private: | |
std::string data; | |
mutable int accessCount; // Can be modified in constant methods | |
}; | |
int main() { | |
Data d("Sample"); | |
d.displayData(); | |
d.displayData(); | |
return 0; | |
} | |
``` |
```shell | ||
Data: Sample, Access count: 1 | ||
Data: Sample, Access count: 2 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a sentence to introduce the output:
```shell | |
Data: Sample, Access count: 1 | |
Data: Sample, Access count: 2 | |
``` | |
The above code produces the following output: | |
```shell | |
Data: Sample, Access count: 1 | |
Data: Sample, Access count: 2 | |
``` |
Data: Sample, Access count: 2 | ||
``` | ||
|
||
Here, even though `displayData` is a const member function, `accessCount` can be incremented due to its `mutable` declaration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make some minor changes:
Here, even though `displayData` is a const member function, `accessCount` can be incremented due to its `mutable` declaration. | |
Here, even though `displayData()` is a constant member function, `accessCount` can be incremented due to its `mutable` declaration. |
|
||
## Codebyte Example | ||
|
||
Use this example to experiment with mutable variables in C++. Enjoy coding! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change the sentence:
Use this example to experiment with mutable variables in C++. Enjoy coding! | |
The following codebyte example demonstrates the usage of mutable variables: |
```codebyte/cpp | ||
#include <iostream> | ||
|
||
class Counter { | ||
public: | ||
Counter() : count(0) {} | ||
|
||
void increment() const { | ||
++count; // count is mutable, so this modification is allowed in const method | ||
} | ||
|
||
int getCount() const { | ||
return count; | ||
} | ||
|
||
private: | ||
mutable int count; | ||
}; | ||
|
||
int main() { | ||
Counter counter; | ||
counter.increment(); | ||
counter.increment(); | ||
std::cout << "Count: " << counter.getCount() << std::endl; | ||
return 0; | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change the indentation to 2 spaces and make some minor changes:
```codebyte/cpp | |
#include <iostream> | |
class Counter { | |
public: | |
Counter() : count(0) {} | |
void increment() const { | |
++count; // count is mutable, so this modification is allowed in const method | |
} | |
int getCount() const { | |
return count; | |
} | |
private: | |
mutable int count; | |
}; | |
int main() { | |
Counter counter; | |
counter.increment(); | |
counter.increment(); | |
std::cout << "Count: " << counter.getCount() << std::endl; | |
return 0; | |
} | |
``` | |
```codebyte/cpp | |
#include <iostream> | |
class Counter { | |
public: | |
Counter() : count(0) {} | |
void increment() const { | |
++count; // 'count' is mutable, so this modification is allowed in this constant method | |
} | |
int getCount() const { | |
return count; | |
} | |
private: | |
mutable int count; | |
}; | |
int main() { | |
Counter counter; | |
counter.increment(); | |
counter.increment(); | |
std::cout << "Count: " << counter.getCount() << std::endl; | |
return 0; | |
} | |
``` |
Description
Add new concept entry term for cpp mutable variables.
Issue Solved
Closes #5473
Type of Change
Checklist
main
branch.Issues Solved
section.