Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limitations of the conditional operator ?:

I am using GCC 4.5 and have observed very peculiar behavior. I am wondering if there is something with this operator that I do not completely understand. I thought I was proficient in C++. I have a thin C++ wrapper class Wnd for Windows HWND objects with an implemented cast operator operator HWND ....

If I use the conditional operator like this (given input Wnd *p and a sample function SetParent(HWND)):

SetParent((p!=NULL) ? (HWND)(*p) : NULL)

The parent is properly set to NULL or p depending. This is what I would expect. However if dare to be lazy and write:

SetParent(p ? *p : NULL)

things go haywire. After running GDB I find that destructor is called on variable p after the call to SetParent. Any ideas what is going on here?

Edit Here is my Wnd class:

class Wnd{
        HWND m_hwnd;        ///< the actual handle
        WndFake *fake;      ///< store state here if we do not have a handle
    public:
        virtual ~Wnd();
        //contructor s
        Wnd(HWND wnd=NULL):m_hwnd(wnd),fake(NULL){}
        Wnd(DWORD sty,const jchar *title,const RECT &sz);
        operator HWND(){return m_hwnd;}
        operator HWND() const {return m_hwnd;}
    }
like image 990
orion93 Avatar asked May 31 '11 00:05

orion93


People also ask

Is ?: A conditional operator?

The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows: The first operand is implicitly converted to bool . It is evaluated and all side effects are completed before continuing.

What is ?: In JavaScript?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What are the 3 conditional operators?

There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary operator.

What is the advantages of conditional operator?

The Conditional Operator's main advantage is that it can be completed in only one sentence, whereas the if-else statement uses multiple code lines.


1 Answers

I suspect that your Wnd has non-explicit conversion constructor too that takes HWND or even int? If so then make it explicit.

Your Wnd probably does not have copy constructor and operator= declared? declare these private and don't define them.

Also remove operator HWND and add member function HWND hwnd() const; to your Wnd. Then the code will look readable like:

Setparent( p ? p->hwnd() : NULL );

I trust that when these mods are done you will find out what is wrong with your Wnd.

The problem manifests itself because the operands at both sides of : in ?: have to be of same type so NULL (0) is somehow convertible to Wnd. So the temporary copy of *p is made as return value of ?: then operator HWND is called to it.

like image 153
Öö Tiib Avatar answered Sep 20 '22 10:09

Öö Tiib