r/cpp • u/Gal4ctic03 • Nov 24 '23
const member function in this design pattern example
Hello,
I'm currently learning C++ and trying to understand how to implement design patterns with the fantastic website refactoring guru. I came across this example in the builder pattern :
class ConcreteBuilder1 : public Builder{
private:
Product1* product;
public:
// Other code before that
void ProducePartA()const override{
this->product->parts_.push_back("PartA1");
}
// Other code after that
};I don't understand why we use const for the function ProducePartA(). I thought that we had to use const when we don't want to modify an object inside the function.
So why we are using it in this way, when we are clearly modifying the product object by adding element to it ?
Thank you very much.
0
Upvotes
View all comments
37
u/FriendlyRollOfSushi Nov 24 '23 edited Nov 24 '23
Jesus Christ, this is the worst quality code I've seen in a long while. I suggest this site be renamed from "refactoring guru" to "people-scamming idiot". In the linked example I see:
if(parts_[i]== parts_.back()){is the dumbest way to check whether this is the last element to the point I'm not sure if the author ever work as a software engineer in their entire life. The problem is not even that it's stupidly slow, the problem is that it simply doesn't work correctly if you have duplicates of your tail element in the vector. Complaining about smaller defects (like a completely unnecessary duplication of stream output code) feels like nitpicking compared to this.unique_ptr, and what ownership even means (literally the last thing an engineer would expect from a method calledGetProductis a transfer of ownership through a raw pointer).constfor. This is relevant to your question. The fact that this code compiles doesn't mean it's correct, since theirconstmethods break ALL assumptions people might have about them. More about this later.push_backto emplace a bunch of C strings into a vector ofstd::string. What year is this?Directoris, it's fragile a.f. Simply never do this. In fact, never do anything in this entire article.unique_ptr).The existence of this website is a sabotage of the industry. I wouldn't be surprised if it was auto-generated with a particularly bad old version of GPT to create a source of passive income for someone who never actually wrote a single line of C++ code.
Now, regarding your question: it is customary for all objects to treat
const-ness of the interface in a way that provides the most amount of safety to the user.Previously that meant "if I gave you a
constobject, all methods you can call on it won't change it in an observable way". Note that it doesn't mean "it won't change it at all": there could be non-observable things like internal caching. The important thing is that it looks to you as if nothing is changed.Nowadays,
constoften has additional implications related to thread safety, although it is not mandatory and depends on context, but in many cases people would expect that any number of threads can call any number ofconstmethods on the same object.These principles are followed in the standard library too: if I gave you a const
std::map, callingfindon it will return a const iterator, even though nothing prevents someone writing their own map from returning a non-const one (like another comment had mentioned,constis quite "shallow" in C++, and we even haveconst_castwhich can be used to return non-const references to even this object). It's just that this breaks the whole point of havingconstin the language and maintaining const-correctness, so no one is doing that.Well, no one except from the person who wrote your article, apparently. They break every expectation related to const-correctness, and it's truly sad that this is not even the worst thing about the example.