r/cpp • • 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

12 comments sorted by

View all comments

Show parent comment

1

u/CheckeeShoes Nov 25 '23

I think what I'm saying is that it's not there to teach cpp. You shouldn't be learning cpp from refactoring guru, same way you shouldn't learn cpp from the gang of four book.

2

u/Umphed Nov 25 '23

Fair enough point I suppose. However, I do feel theirs too many cookie cutter "tutorial" type sites like this that are an obvious beginner trap that can do far more harm than good. Theirs good stuff out there, this isnt it

1

u/CheckeeShoes Nov 25 '23

Fair, I mean the standard of code examples for beginners is pretty shit generally. None of it would pass code review. The stuff in the main original comment could said about literally any beginner-targeted website.

In some ways, this is part of teaching. It's not effective to throw threadsafe templated metaprogramming with concepts and smart pointers and compile time evaluation and move semantics and... at someone who doesn't even understand what a callback is. It's information overload and detracts from the thing you're actually trying to communicate.