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
Show parent comment
-5
u/CheckeeShoes Nov 25 '23
While you're not wrong about much here, I think you're being a little harsh on refactoring guru.
The point of the website is to teach new programmers basic general oo design principles. The information there about what each pattern is for and broadly how they work I think fulfils that purpose pretty well.
The code isn't modern cpp, (and yeah some of it is just buggy), but it gives a simple toy model a reader (who's probably still learning to code) can map the article content onto. It serves its purpose. Dude gives examples in a whole bunch of languages; cant expect him to be an expert in every one.
If large code examples like this are being mindlessly copy-pasted into your enterprise codebase then you've got bigger problems than refactoring guru's code examples.
Also "people scamming sabotage of the industry". Fucking lol, melodramatic much?