Typedef naming for boost :: shared_ptr <const Foo>

Stupid question, but say you have a Foo class:

class Foo
{
public:
    typedef boost::shared_ptr<Foo> RcPtr;

    void non_const_method() {}
    void const_method() const {}
};

The presence of const Foo :: RcPtr does not prevent the invocation of non-constant methods in the class, the following will compile:

#include <boost/shared_ptr.hpp>

int main()
{
    const Foo::RcPtr const_foo_ptr(new Foo);
    const_foo_ptr->non_const_method();
    const_foo_ptr->const_method();

    return 0;
}

But to indicate typedef ConstRcPtr for me, it implies that typedef will be

typedef const boost::shared_ptr<Foo> ConstRcPtr;

that is not what interests me. A stranger name, but perhaps more accurate, is RcPtrConst:

typedef boost::shared_ptr<const Foo> RcPtrConst;

However, Googling for RcPtrConst gets null hits, so people don’t use this as a typedef name :)

Does anyone have any other suggestions?

+3
source share
3 answers

typedef , . typedef . , const T const_iterator, const ( ). , , , :)

typedef T const* const_iterator;

, , . , Const , , . , ,

typedef boost::shared_ptr<Foo const> ConstRcPtr;

, std::allocator<>::const_pointer.

+4

, const, : typedef. Foo:: TPtr Foo:: TConstPtr typedefs.

+4

typedef ConstRcPtr , typedef typedef const boost::shared_ptr<Foo> ConstRcPtr;

? " " ( ), const char*, char *const. const_iterator - -, , , , ConstRcPtr RcPtr const_iterator iterator.

If you need to distinguish between const char*and char *constthen yes, you can call char *consta "const pointer" and const char*a "pointer-to-const". But you almost never do this, so the short phrase "const pointer" is used for the general case. I think this applies here if you do not plan to introduce all four of the following:

boost::shared_ptr<Foo>
boost::shared_ptr<const Foo>
const boost::shared_ptr<Foo>
const boost::shared_ptr<const Foo>

In this case, you should be a little more creative with the names.

+1
source

Source: https://habr.com/ru/post/1745846/


All Articles