Re: Can I overload with unused arguments to make the code clear?
On Thursday, 14 March 2013 13:35:05 UTC+2, DeMarcus wrote:
On 2013-03-13 17:35, ?? Tiib wrote:
On Wednesday, 13 March 2013 00:50:06 UTC+2, DeMarcus wrote:
If it is dynamic polymorphism or dynamic dispatch then get out of
immersion of nullptr. Make a polymorphic smart pointer that instead
of nullptr uses pointer to static "MissingEntertainer" object that
*implements* the interface of Entertainer (just does nothing). Such
pointer can then never be nullptr, can be always dereferenced etc.
I'd call it as robust_pointer. :-)
I agree your solution is better, but you can still accidently provide
nullptr, right?
Because of new loose semantics of list-initializes I currently seem to
make all constructors besides default, copy and move 'explicit'.
With smart pointers I additionally tend to have factory functions
and prefer those when creating the pointers:
template<class T, class None, /*variadic template parameters*/>
robust_ptr<T,None> make_robust(/*variadic function parameters*/)
{/*usual stuff*/ return ret; }
typedef EntertainerPtr robust_ptr<Entertainer, MissingEntertainer>;
That makes it quite hard that nullptr sneaks in from somewhere
silently and if it comes explicitly from front doors then it will be
converted into MissingEntertainer*.
So I'm still wondering, in order to prevent nullptr to
be used, is it good practice to do this?
void watchShow( std::nullptr_t ) = delete;
But the EntertainerPtr can't never be nullptr.
You will have only that:
void watchShow( Entertainer& );
Usage is like that:
Audience a;
EntertainerPtr p; //default constructs to MissingEntertainer*
a.watchShow( *p );
Works like charm.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]