const int *p vs. int const *p - Is const after the type acceptable?

前端 未结 15 1695
萌比男神i
萌比男神i 2020-12-08 05:01

My co-worker is 0 for 2 on questions he has inspired (1, 2), so I thought I\'d give him a chance to catch up.

Our latest disagreement is over the style issue of wher

15条回答
  •  情深已故
    2020-12-08 05:07

    I agree with both of you. You should put the const after the type. I also find looking at it an abomination that must be destroyed. But my recent foray into the wonders of const value parameters has made me understand why putting the const second makes sense.

    int *
    int const *
    int * const
    int const * const
    

    Just looking at that has the hairs on my neck standing. I'm sure it would confuse my co-workers.

    EDIT: I was just wondering about using this in classes:

    class Foo {
        Bar* const bar;
        Foo(const Foo&) = delete; // would cause too many headaches
    public:
        Foo() : bar(new Bar) {}
        ~Foo() { delete bar; }
    };
    

    bar in this example is functionally equivalent to Bar& but it is on the heap and can be deleted. For the lifetime of each Foo, there will be a single Bar associated with it.

提交回复
热议问题