三元算子和加法优先

时间:2016-02-23 05:30:50

标签: c++ operator-precedence

根据此处的优先顺序,有些人可以解释以下内容如何工作:http://en.cppreference.com/w/cpp/language/operator_precedence

假设:

#include <cstdio>

#define MY_CONSTANT 5.6

int main(int argc, char ** argv) {
    const double calculatedValue = 4.4;
    const double myValue = MY_CONSTANT + 1 ? 4.4 : -4.4;

    printf("%f\n", myValue);
    return 1;
}

我希望

myValue == 10

我得到了

myValue == 4.4;

1 个答案:

答案 0 :(得分:2)

const double myValue = (MY_CONSTANT + 1) ? 4.4 : -4.4;

相当于:

?:

因为+的前导值低于C ++运算符优先级表中的(MY_CONSTANT + 1)

由于4.4评估为非零,因此myvalue为10

要将const double myValue = MY_CONSTANT + (1 ? 4.4 : -4.4); // ^ ^ 作为输出,您需要显式括号来更改评估顺序:

<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

    <title>Ionic Template</title>

    <link href="http://code.ionicframework.com/1.0.0-beta.4/css/ionic.css" rel="stylesheet">
    <script src="http://code.ionicframework.com/1.0.0-beta.4/js/ionic.bundle.js"></script>
  </head>
  <body >

    <ion-nav-view></ion-nav-view>

    <script id="app.html" type="text/ng-template">
<ion-side-menus>

  <ion-pane ion-side-menu-content>
    <ion-nav-bar class="bar-stable nav-title-slide-ios7">
      <ion-nav-back-button class="button-clear"><i class="icon ion-chevron-left"></i> Back</ion-nav-back-button>
    </ion-nav-bar>
    <ion-nav-view name="appContent" animation="slide-left-right"></ion-nav-view>
  </ion-pane>

  <ion-side-menu side="left">
      <ion-nav-view name="menuList"></ion-nav-view>
  </ion-side-menu>
</ion-side-menus>

    </script>    

    <script id="browse.html" type="text/ng-template">
<ion-view title="Browse">
  <ion-nav-buttons side="left">
    <button menu-toggle="left"class="button button-icon icon ion-navicon"></button>
  </ion-nav-buttons>
  <ion-content class="has-header">
    <h1>Browse</h1>
  </ion-content>
</ion-view>

    </script>    

    <script id="menuBrowse.html" type="text/ng-template">
<header class="bar bar-header bar-stable">
    <h1 class="title">First Menu</h1>
</header>
<ion-content class="has-header">
    <ion-list>
        <ion-item nav-clear menu-close href="#/app/search">
            Search
        </ion-item>
        <ion-item nav-clear menu-close href="#/app/playlists">
            Second Menu
        </ion-item>
    </ion-list>
</ion-content>

    </script>    


    <script id="menuPlaylists.html" type="text/ng-template">
      <header class="bar bar-header bar-stable">
    <h1 class="title">Second Menu</h1>
</header>
<ion-content class="has-header">
    <ion-list>
        <ion-item nav-clear menu-close href="#/app/search">
            Search
        </ion-item>
        <ion-item nav-clear menu-close href="#/app/browse">
            Different Menu
        </ion-item>
    </ion-list>
</ion-content>

    </script>    


    <script id="menuSearch.html" type="text/ng-template">
<header class="bar bar-header bar-stable">
    <h1 class="title">Search Menu</h1>
</header>
<ion-content class="has-header">
    <ion-list>
        <ion-item nav-clear menu-close href="#/app/browse">
            Browse
        </ion-item>
        <ion-item nav-clear menu-close href="#/app/playlists">
            Playlists
        </ion-item>
    </ion-list>
</ion-content>

    </script>    

    <script id="playlist.html" type="text/ng-template">
<ion-view title="Playlist">
  <ion-content class="has-header">
    <h1>Playlist</h1>
  </ion-content>
</ion-view>

    </script>    

    <script id="playlists.html" type="text/ng-template">
<ion-view title="Playlists">
  <ion-nav-buttons side="left">
    <button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
  </ion-nav-buttons>
  <ion-content class="has-header">
    <ion-list>
      <ion-item ng-repeat="playlist in playlists" href="#/app/playlists/{{playlist.id}}">
        {{playlist.title}}
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

    </script>    

    <script id="search.html" type="text/ng-template">
<ion-view title="Search">
  <ion-nav-buttons side="left">
    <button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
  </ion-nav-buttons>
  <ion-content class="has-header">
    <h1>Search</h1>
  </ion-content>
</ion-view>

    </script>    

    <script id="" type="text/ng-template">

    </script>    

    <script id="" type="text/ng-template">

    </script>    



  </body>
</html>