簡體   English   中英

使用指向成員和宏的指針

[英]Using pointers to members and macros

我在使用以下代碼時遇到問題:

#include<iostream>
#include<utility>

#define Row *prow
#define Col *pcol

typedef unsigned int uint;
typedef std::pair<uint, uint> Node;

uint Node::Row = &Node::first;
uint Node::Col = &Node::second;

int main()
{
    Node node(1,2);
    std::cout << node.*prow << node.*pcol << '\n';    // OK
    std::cout << node.Row << node.Col << '\n';        // doesn't compile
}

想法是使用std :: pair,但用其他名稱替換firstsecond ,在這種情況下為RowCol 但是,即使最后一行應與前一行完全相同,也不會編譯。 如果有人能解釋我為什么會發生,我將非常感謝。 我正在使用VS2015。

編輯:編譯器錯誤C2059 syntax error:'*'

.*是單個令牌。 您的宏正在生成兩個相鄰的標記. * ,這不是同一回事。 (或者至少可以這樣做。幾乎可以肯定,您正在調用未定義的行為。)

可能存在涉及令牌粘貼的解決方案,但是您將不使用C ++中的宏就可以幫自己一個大忙(並使Bjarne開心)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM