c++ - returning bool from member variable pointer -
so i'm working code looks this:
class someclass : public somebaseclass { somepointertoclass * p; public: someclass(somepointertoclass* p = null) : p(p) {}; void start(somepointertoclass* sp) { p = sp; } bool hasstarted() { return p; } }
i simplified stuff make short code example, think returning bool member variable pointer bit of codesmell. should change or form of c++ convention?
hasstarted()
fine: in c++, pointer automatically converted bool
on zero/not-zero basis.†
some people prefer write return (p != nullptr)
or return static_cast<bool>(p)
instead, in order explicit intent, i'm not terribly fussy in cases this.
† however, behaviour implementation-defined in c, not rely on there!
Comments
Post a Comment