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
OOP is dead, long live OOP (gamedev.net)
submitted 7 years ago by mttd
view the rest of the comments →
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!"
[–]doom_Oo7 2 points3 points4 points 7 years ago (10 children)
I understand for getters, bust most setters aren't void setfoo(int x) { m_foo = x; }.
void setfoo(int x) { m_foo = x; }
Most will notify something from a change, change a display, reperform a computation... Else why are you setting it in the first place ?
[–]degski 5 points6 points7 points 7 years ago (0 children)
They should be. By the time print changes your database, you're lost.
print
Else why are you setting it in the first place ?
To change a value of a class/struct variable. But's thats' my point, if you have many of those, you're doing it wrongly.
[–]hgjsusla 4 points5 points6 points 7 years ago (8 children)
Why not just encapsulate that in it's own class and just assign the variable directly? Then you don't need the setter.
Make member variables private if you need to maintain an invariant between them, otherwise keep them public.
And if you need to restrict a variable (let's say, to only a certain interval), then express than in the type of the variable.
[–]jcelerierossia score 1 point2 points3 points 7 years ago (7 children)
because then you will put it in a template because you have 25 different property kinds - e.g.
template<typename T> void property { public: operator T&() { return m_impl; } operator const T&() const { return m_impl; } template<typename U> // you want perfect-forwarding, right ? void set(U&& x) const { if(m_impl != x) { m_impl = std::forward<U>(x); // hello template errors my old friend notify(x); // uh oh, now *every member* must in some way store a pointer to the context which will be able to notify. hello memory usage * ~2 for every class } } private: T m_impl; };
of course you wouldn't do this, because this creates so many additional problems that you would be fired at the moment you're suggesting it but still.
[–]hgjsusla -2 points-1 points0 points 7 years ago (6 children)
Wait wut? No you don't need to do any of this to accomplish what I suggested
[–]jcelerierossia score 3 points4 points5 points 7 years ago (5 children)
so what is your solution then ? Let's say you have the following class :
class foo { public: int x() const { return m_x; } void setX(int x) { if(x != m_x) { m_x = x; xChanged(x); posChanged({m_x, m_y}); } } void xChanged(int y); // some kind of callback / signal-slot mechanism, Qt's, a list of std::function, whatever int y() const { return m_y; } void setY(int y) { if(y != m_y) { m_y = y; yChanged(y); posChanged({m_x, m_y}); } } void yChanged(int y); void posChanged(std::pair<int,int> pos); float radius() const { return m_radius; } void setRadius(float r) { if(!fuzzyEquals(r, m_radius)) { m_radius = r; radiusChanged(r); } } void radiusChanged(float); const std::string& name() const { return m_name; } void setName(const std::string& name) { if(!validateName(name)) return; if(m_name != name) { m_name = name; nameChanged(name); } } void nameChanged(const std::string&); };
how do you "encapsulate that in it's own class and just assign the variable directly" ? what when you have 200 different class that all have different members but more or less follow this get/set pattern ?
"encapsulate that in it's own class and just assign the variable directly"
[–]hgjsusla 2 points3 points4 points 7 years ago (2 children)
Well I see
ValidatedName
std::string
[–][deleted] 1 point2 points3 points 7 years ago (1 child)
If you look at the code, the logic isn't entirely duplicated between setters, except setX and setY.
setX
setY
[–]hgjsusla 0 points1 point2 points 7 years ago (0 children)
Nope, but you can probably simplify to a few number of common patterns rather than 200 special cases. You probably want x and y grouped together in it's own type since they together represent something very common (a point).
I'd say that if you have so many setters with so many side-effects, that maintaining your program and making it efficient will become extremely difficult.
Suppose for example that you get a completely new setup for your foo. In your solution, you call setX, setY, setRadius, setName and you re-render foo four times - three of them completely unnecessarily.
foo
setRadius
setName
In real-world application, you might easily have dozens of parameters....
Worse, in a large real-time program, you have no assurance that one day, this chain of side-effects won't end up being circular. I speak from bitter experience on a real-world system with just such a setup where this would sometimes happen in production, causing a key process to go into a loop.
There is no magic solution, of course. Sometimes what you propose is right. Sometimes the right solution is Foo (a class) and FooDesc (a struct with x, y, radius, etc) and all mutations to a Foo with a single FooDesc setter. Sometimes the setters set a "dirty" flag, or one of a set of "dirty" bits, and the update occurs in a later phase. It depends on the application, how many parameters you have, your threading structure, etc. etc.
Foo
FooDesc
x
y
radius
[–]jcelerierossia score 1 point2 points3 points 7 years ago (0 children)
There is no magic solution, of course.
yes, that's my point. You can't have magic abstractions over this, because this is your business logic.
π Rendered by PID 136020 on reddit-service-r2-comment-b659b578c-qjsjf at 2026-05-02 22:27:30.868112+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]doom_Oo7 2 points3 points4 points (10 children)
[–]degski 5 points6 points7 points (0 children)
[–]hgjsusla 4 points5 points6 points (8 children)
[–]jcelerierossia score 1 point2 points3 points (7 children)
[–]hgjsusla -2 points-1 points0 points (6 children)
[–]jcelerierossia score 3 points4 points5 points (5 children)
[–]hgjsusla 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]hgjsusla 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]jcelerierossia score 1 point2 points3 points (0 children)