ostream operator<<致电父ostream

时间:2016-01-13 12:10:04

标签: c++ operator-keyword ostream

代码:

cout << "11122333" << endl;

期望:
11122333个\ n
结果:
11122333个\ n
行。
代码:

cout.operator<<("11122333");
cout.operator<<(endl);

期望:
11122333个\ n
结果:
00B273F8 \ n
(或其他一些地址,它被投射到void* :()

麻烦: 想要从ostream

派生的写类
class SomeStream : public ostream
{
  public:
  explicit SomeStream(streambuf* sb) : ostream(sb) { }
  template <typename T> SomeStream &operator <<(const T &val) 
  {
    std::ostream::operator<<(val); //Trouble in there!
    std::cout << "<<" << " " << typeid(T).name() << " " << val << std::endl;
    /*some other behavior*/
    return *this;
  }
  SomeStream &operator <<(ostream& (*val) (ostream&))
  {
    std::ostream::operator<<(val);
    /*some other behavior*/
    return *this;
  }
  SomeStream &operator <<(ios_base& (*val) (ios_base&))
  {
    std::ostream::operator<<(val);
    /*some other behavior*/
    return *this;
  }
};

当我将父操作符std::ostream::operator<<(val); val调用void*而不是正常工作时。 怎么做?以及operator<<的直接调用ostream与间接调用的工作方式不同。

1 个答案:

答案 0 :(得分:3)

const char*的输出ostream不是void*类型的成员。 Only those重载是成员函数,其中一个用于 template <typename T> SomeStream &operator <<(const T &val) { static_cast<std::ostream&>(*this) << val; //Trouble in there! std::cout << "<<" << " " << typeid(T).name() << " " << val << std::endl; /*some other behavior*/ return *this; } 。 还有non-member overloads

有解决方法:

<script>
    // Parse the response and build an HTML table to display search results

    function _cb_findItemsByKeywords(root) {
        var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || [];
        var html = [];
        var itemCount = items.length;
        var showNo = 4;


                function carousel_4(){
                     html.push(' <div class="col-md-3 col-xs-12 text-middle modalfix">' +
                        '<a href="' + viewitem + '" ><img class = "img-rounded img-middle limiter ebaypic" src="' + pic + '" alt=""/></a>' +
                        '<label class="inside-pic  mh300">' +
                            '<h2 class = "text-red">' + '&pound;' + floatPrice + '</h2>' +  
                            '<div class = "mh75">' +
                                '<p>' + title + '</p>' +
                            '</div>' +
                            '<p class = "text-red"><i class="fa fa-hourglass-end fa-spin"></i>&nbsp; Sale ends in: 1d 23h 6min</p>' +
                        '</label>' +               
                        '<button href="' + viewitem + '" class = "btn btn-default btn-lg col-md-12 col-xs-12 mb25">' +
                             //'<i class = "fa fa-lg fa-arrow-up pull-left"></i>' +
                                'VIEW' +
                            // '<i class = "fa fa-lg fa-arrow-up pull-right"></i>' +
                        '</button>' + 
                    '</div>');
                }


//Does the thing here
for (var i = 1; i <= 12; ++i) {

        var item     = items[i];
        var title    = item.title;
        var pic      = item.galleryURL;
        var viewitem = item.viewItemURL;
        //var price    = item.currentPrice;
        var price = item.sellingStatus[0].currentPrice[0]["__value__"];
        var floatPrice = parseFloat(price);
        var activeClass = "active";
        if (null != title && null != viewitem) 
        {
            if(i == 1 || i == 5 || i == 9) {
                if (i > 1){
                var activeClass = " ";
                }
                html.push('<div class="carousel-padding item ' + activeClass +' ">');
            }

            carousel_4();

            if(i == 4 || i == 8 || i == 12) {
                html.push('</div>');
            }
        }
}


      document.getElementById("results").innerHTML = html.join("");
    }  // End _cb_findItemsByKeywords() function

        // Create a JavaScript array of the item filters you want to use in your request
        var filterarray = [
          {"name":"MaxPrice",
           "value":"60000",
           "paramName":"Currency",
           "paramValue":"GBP"},
          {"name":"FreeShippingOnly",
           "value":"false",
           "paramName":"",
           "paramValue":""},
          {"name":"accountId",
           "value":"8portrides",
           "paramName":"",
           "paramValue":""},
          {"name":"ListingType",
           "value":["AuctionWithBIN", "FixedPrice"],
           "paramName":"",
           "paramValue":""},
          ];
        // Define global variable for the URL filter
        var urlfilter = "";

        // Generates an indexed URL snippet from the array of item filters
            function  buildURLArray() {
              // Iterate through each filter in the array
              for(var i=0; i<filterarray.length; i++) {
                //Index each item filter in filterarray
                var itemfilter = filterarray[i];
                // Iterate through each parameter in each item filter
                for(var index in itemfilter) {
                  // Check to see if the paramter has a value (some don't)
                  if (itemfilter[index] !== "") {
                    if (itemfilter[index] instanceof Array) {
                      for(var r=0; r<itemfilter[index].length; r++) {
                      var value = itemfilter[index][r];
                      urlfilter += "&itemFilter\(" + i + "\)." + index + "\(" + r + "\)=" + value ;
                      }
                    }
                    else {
                      urlfilter += "&itemFilter\(" + i + "\)." + index + "=" + itemfilter[index];
                    }
                  }
                }
              }
            }  // End buildURLArray() function

            // Execute the function to build the URL filter
            buildURLArray(filterarray);

        // Construct the request
        // Replace MyAppID with your Production AppID
        var url = "http://svcs.ebay.com/services/search/FindingService/v1";
            url += "?OPERATION-NAME=findItemsByKeywords";
            url += "&SERVICE-VERSION=1.0.0";
            url += "&SECURITY-APPNAME=ctodsalv-3008-430d-8288-a23f10d6382a";
            url += "&GLOBAL-ID=EBAY-GB";
            url += "&RESPONSE-DATA-FORMAT=JSON";
            url += "&callback=_cb_findItemsByKeywords";
            url += "&REST-PAYLOAD";
            //url += "&keywords=car%20parts";
            url += "&keywords=1.6%20engine";
            url += "&paginationInput.entriesPerPage=20";
            url += "&sortOrder=EndTimeSoonest";
            url += urlfilter;

            // Submit the request
            s=document.createElement('script'); // create script element
            s.src= url;
            document.body.appendChild(s);
</script>