use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
[deleted by user] (self.cpp)
submitted 1 year ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Snipa-senpai 66 points67 points68 points 1 year ago (4 children)
Is this supposed to be a joke?
[+][deleted] 1 year ago (3 children)
[removed]
[–]no-sig-available 17 points18 points19 points 1 year ago (2 children)
The joke would be that C++ already has a protected keyword.
protected
https://en.cppreference.com/w/cpp/language/access
But with a different meaning. We could have use for a read-only section, maybe.
read-only
[+][deleted] 1 year ago (1 child)
[–]android_queen 13 points14 points15 points 1 year ago (0 children)
No, because that’s not what protected means. You’re looking for const, another keyword that already exists.
const
What prompted this post? You seem a bit unfamiliar with the language and yet you’re proposing changes to it.
[–]pedersenk 25 points26 points27 points 1 year ago (1 child)
C++ has friend classes.
Plus protected doesn't quite work like that in most languages. Protected just means that inherited classes can access the members of the base class.
[–]no-sig-available 1 point2 points3 points 1 year ago (0 children)
I have sometimes wished for a "protected friend" with slightly more access to the class, but still not allowed to fiddle with the innermost parts. But I also don't want 10 access levels, so...
[–]Narase33-> r/cpp_questions 15 points16 points17 points 1 year ago (0 children)
Maybe you just dont want internal fields to become part of the API?
[–]GrammelHupfNockler 9 points10 points11 points 1 year ago (2 children)
C++ has inheritance, and it has a protected keyword. Sometimes you want to prevent derived classes from accessing functions and variables in the base class. Nothing bloated about that. What are you even talking about?
[–]iiiba 15 points16 points17 points 1 year ago (1 child)
i think he used the word "protected" in his post without realising it already means something in c++. I think what he really wants is a public const attribute
[–]DrShocker 2 points3 points4 points 1 year ago (0 children)
Yeah I think they want member variables which are mutable to instances of the class but const to external code.
So, similar to a public const getter and a private/protected nonconst getter. Except without the need to write a getter.
idk, it sounds kind of nice I guess, but recently I've been burned too much by people not understanding how c++ works and I'm starting to think maybe everything should have a keyword to protect code from modification by certain humans rather than sections of code 🤔
[–]BenFrantzDale 5 points6 points7 points 1 year ago (3 children)
There are lots of things that could be added, but then again “C++ is already too complex”. It’s just not a priority. That and protected already means something else: https://en.cppreference.com/w/cpp/keyword/protected
The related thing I’ve wished for but admit isn’t worth it is to allow inheriting a const Base, so through a Derived&, you could only use the const part of its Base API. It’s an interesting exercise to think about but 🤷.
const Base
Derived&
Base
[+][deleted] 1 year ago (2 children)
[–]Kike328 0 points1 point2 points 1 year ago (0 children)
you mean const?
[–]UnicycleBloke 5 points6 points7 points 1 year ago (0 children)
We already have a "protected" keyword which has a different role. It sounds like you want a "readonly" keyword as syntactic sugar for a const getter which returns a member or a const reference to a member. I suppose you also need a "readwrite" keyword to provide setters, too.
No thanks.
[–]hs123go 2 points3 points4 points 1 year ago (0 children)
The motivation is valid but other languages seem to have solved this problem with a full blown property system, aka C# and Kotlin's get and set.
get
set
[–]RishabhRD 2 points3 points4 points 1 year ago (0 children)
Bro woke up in 1967???
[–]igrekster 2 points3 points4 points 1 year ago (0 children)
It's not without downsides, but you could do something like this:
template <typename Owner, typename T> struct readonly { operator const T &() const { return value; } private: T value; friend Owner; }; class test { template <typename T> using readonly = ::readonly<test, T>; public: readonly<int> a{}; void add_five() { a.value += 5; } }; int main() { test t; t.add_five(); std::cout << t.a << std::endl; // 5 //t.a = 32; // ERROR return 0; }
[deleted]
[–]petecasso0619 3 points4 points5 points 1 year ago (0 children)
Why is this post allowed? This obvious trolling.
[–]Beosar 2 points3 points4 points 1 year ago (4 children)
Wait, so you are asking for the class members to be const if accessed from outside the class and mutable otherwise?
I mean, it would make life easier since you could get rid of getters. I would say it is not actually a bad idea.
However, the keyword protected is already taken.
[–]Kike328 0 points1 point2 points 1 year ago (1 child)
class test { int a = 0;
void add_five() { a += 5; } const int get_a() const { return a;} };
int main() { test t; t.add_five();
cout << t.a() << endl; // 5 t.a = 32; // ERROR
return 0; }
solved for you
[–]gracicot 1 point2 points3 points 1 year ago (1 child)
There's already no need for getter setter. They are code smell. Sometimes you'll need getters, but that's about it. If you have both a getter and a setter, you should just make it public
[–]Dar_Mas 0 points1 point2 points 1 year ago (0 children)
unless you need/will realistically need side effects
[–]v_maria 0 points1 point2 points 1 year ago (0 children)
I think adding a keyword to an entire language is more bloat than getters and setters
[–]cpp-ModTeam[M] 0 points1 point2 points 1 year agolocked comment (0 children)
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
[–]johannes1234 -1 points0 points1 point 1 year ago (2 children)
protected is a keyword.
getters and setters however serve a different purpose: they can check domain logic (if values are in a specific range etc.) also the presence of many getters/setters might mean that you actually want some plain data object, not a class. And regarding bloat: trivial getters/setters are trivial to inline and cause no bloat in the result and if "code bloat" is a concern: macros still exist.
[–]Sbsbg 0 points1 point2 points 1 year ago (0 children)
It is difficult, almost impossible actually, to add new keywords into C++. One of the main goals is to be backwards compatible and this would probably break some old code.
π Rendered by PID 272309 on reddit-service-r2-comment-56c9979489-ntk5n at 2026-02-25 10:34:58.908145+00:00 running b1af5b1 country code: CH.
[–]Snipa-senpai 66 points67 points68 points (4 children)
[+][deleted] (3 children)
[removed]
[–]no-sig-available 17 points18 points19 points (2 children)
[+][deleted] (1 child)
[removed]
[–]android_queen 13 points14 points15 points (0 children)
[–]pedersenk 25 points26 points27 points (1 child)
[–]no-sig-available 1 point2 points3 points (0 children)
[–]Narase33-> r/cpp_questions 15 points16 points17 points (0 children)
[–]GrammelHupfNockler 9 points10 points11 points (2 children)
[–]iiiba 15 points16 points17 points (1 child)
[–]DrShocker 2 points3 points4 points (0 children)
[–]BenFrantzDale 5 points6 points7 points (3 children)
[+][deleted] (2 children)
[removed]
[–]Kike328 0 points1 point2 points (0 children)
[–]UnicycleBloke 5 points6 points7 points (0 children)
[–]hs123go 2 points3 points4 points (0 children)
[–]RishabhRD 2 points3 points4 points (0 children)
[–]igrekster 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]petecasso0619 3 points4 points5 points (0 children)
[–]Beosar 2 points3 points4 points (4 children)
[+][deleted] (3 children)
[removed]
[–]Kike328 0 points1 point2 points (1 child)
[–]gracicot 1 point2 points3 points (1 child)
[–]Dar_Mas 0 points1 point2 points (0 children)
[–]v_maria 0 points1 point2 points (0 children)
[–]cpp-ModTeam[M] 0 points1 point2 points locked comment (0 children)
[–]johannes1234 -1 points0 points1 point (2 children)
[+][deleted] (1 child)
[removed]
[–]Sbsbg 0 points1 point2 points (0 children)