作者notBeing (read and be read)
看板C_and_CPP
標題[問題] overload -> of a raw pointer
時間Tue Sep 1 02:23:48 2015
對raw pointer而言,dereference operator是否能夠被覆載?
例如:
class C1
{
public:
std::string str;
void show( void ){ std::cout << str; }
};
template <typename T>
class TC2
{
public:
T* mPtr;
TC2( T* ptr ): mPtr( ptr ){}
T* operator->()
{
return mPtr;
}
};
int main()
{
C1 *pC1 = new C1;
TC2<C1> *pTC2 = new TC2<C1>( pC1 );
//succeed
(*pTC2)->show();
//fail
//pTC2->show();
delete pC1;
delete pTC2;
}
請問
pTC2->show();要怎麼做,才能夠如同(*pTC2)->show();一樣執行?
從結果與相關資訊看來,語意上->的覆載是對一個 物件實體(*pTC2) 有作用
但我沒有找到關於如何對一個raw pointer的->運算子做覆載。
亦或者raw pointer的->運算子是根本不能被覆載的?若是如此,請問在standard中
哪個部分有指出這件事,謝謝!
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.53.96
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1441045432.A.287.html
推 LPH66: operator-> 是做為其左邊式子的 member function 為定義 09/01 03:40
→ LPH66: 其不含參數, 亦無法做為 non-member 定義 09/01 03:41
→ LPH66: 因此無法對 pointer (此為一 primitive type) 覆載 -> 09/01 03:42
→ notBeing: 了解,謝謝。是說raw pointer是primitive type還是 09/01 03:50
→ notBeing: compound type? 09/01 03:50
→ notBeing: 在Standard中13.3.1.2-1有指出若運算元的類別若非class 09/01 22:58
→ notBeing: 或enumeration, 則使用built-in operator 09/01 22:59
推 LPH66: 指標都是 primitive, 故以這理由它依然不能被覆載 09/02 00:38