Note that there are some explanatory texts on larger screens.

plurals
  1. POoverloading operator !=
    text
    copied!<p>I wrote a smarter pointer class. And to make the following code correct </p> <pre><code>ZhjSmartPointer&lt;int&gt; a(new int); assert(a != NULL); </code></pre> <p>I overload the != operator like this: </p> <pre><code>bool operator !=(T *ptr) const; </code></pre> <p>however, this leads to a compile error like this: </p> <blockquote> <p>ZhjSmartPointer.h:132: note: candidate 1: bool ZhjSmartPointer::operator!=(T*) const [with T = Test] test.cpp:41: note: candidate 2: operator!=(int, int) </p> </blockquote> <p>I'm confuse with how a <code>ZhjSmartPointer</code> can be transfered into an <code>int</code></p> <p>The Code of SmartPointer class is like this:</p> <pre><code>template &lt;typename T&gt; class ZhjSmartPointer { public: ZhjSmartPointer(); explicit ZhjSmartPointer(T *ptr); ZhjSmartPointer(const ZhjSmartPointer &amp;smartPtr); ZhjSmartPointer &amp;operator =(const ZhjSmartPointer &amp;smartPtr); ~ZhjSmartPointer(); operator bool() const; T &amp;operator *() const; T *operator -&gt;() const; bool operator ==(const ZhjSmartPointer &amp;smartPtr) const; bool operator !=(const ZhjSmartPointer &amp;smartPtr) const; bool operator ==(T *ptr) const; bool operator !=(T *ptr) const; private: void copyPtr(const ZhjSmartPointer &amp;smartPtr); void deletePtr(); T *ptr_; size_t *refCnt_; }; </code></pre> <p>I guess because I overload the 'bool' operator, 'ZhjSmartPointer -> bool -> int' leads to this problem.Is this right?</p> <p>Sorry,It is just a compile warning, not a error. Someone suggest me not overloading != with parameter(T *), after all, we already have overloaded 'bool'.It will be fine to write codes like these:<br> ZhjSmartPointer a(new int);<br> if (a) { ..........<br> } </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload