2016-01-13 17 views
-1
#include <iostream> 
#define CONCATENATE t; Box(); t 
#define END ; 
using namespace std; 

class Box 
{ 
public: 
     Box(){ } 
     Box(string _txt){ } 
     Box operator|(const Box& b) 
     { 
     cout<<"hi from op |"<<endl; 
     Box box; 
     return box; 
     } 
     void operator+() 
     { 

     } 
public: 
     string str; 
}; 

int main() 
{ 
    Box t; 
    Box tt; 

    CONCATENATE //this works 
    CONCATENATE CONCATENATE //this doesn't work because cant understand tt object 
    END 
} 

如果我給CONCATENATE CONCATENATE編譯器說分號缺少多數正確但編譯器應該理解t;框(); TT;框();因爲在分析階段刪除空格。好奇的類對象連接C++

如果我編寫CONCATENATE END編譯器執行那個並且是正確的 t;框();噸; 如果我編寫CONCATENATE CONCATENATE END編譯器執行那個並且不正確 t;框(); t t;框();噸;因爲了解分號丟失,但在這種情況下,我希望編譯器明白應該執行對象tt並忽略空格。

我該怎麼做那些傢伙?

  • 更新

    預處理器連接了字符串到TT;目的?

    int main(){ 
        Box t; 
        Box tt; 
    
        //this work (is such 2 times CONCATENATE macro) 
        t; Box(); tt; Box(); t END 
        //this doesnt work 
        CONCATENATE CONCATENATE END 
    
    } 
    
+6

你究竟想在這裏實現什麼? [這幾乎肯定是錯誤的做法。](http://meta.stackexchange.com/q/66377/242291) – BoBTFish

+0

檢查我的update.thanks! –

+2

@ jay_gr13你的更新仍不能很好地解釋你對這個構造的用例。對我來說毫無意義。 –

回答

1

至於你「更新」

預處理器連接了字符串到TT;目的?

不,預處理器不會連接任何東西,除非您用## string concatenation preprocessing operation指示它。我做簡單的文本替換,所以讓我們來查找您更換從預處理:

//this work (is such 2 times CONCATENATE macro) 
t; Box(); tt; Box(); t END 
//this doesnt work 
CONCATENATE CONCATENATE END 

將擴大到

//this work (is such 2 times CONCATENATE macro) 
t; Box(); tt; Box(); t ; 
//this doesnt work 
t; Box(); t t; Box(); t ; 

t t;不被視爲有效語法。你的假設將連接到tt是錯誤的。

+0

你已經抓住了我想要的..有可能做出一些解決它? –

+0

@ jay_gr13恐怕不行,除非你引入另一個宏,它帶兩個參數,並使用前面提到的['##'concatenation](https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html)運算符。 –