簡體   English   中英

將基類的引用傳遞給另一個函數

[英]Passing a reference of a base class to another function

這是我面臨的問題,有人可以解決嗎?

Class A: public class B
{
 // I want to pass a reference of B to Function
}

void ClassC::Function(class& B)
{
  //do stuff
}

您聲明類的方式是錯誤的:

class A : public B // no more class keyword here
{

}; // note the semicolon

void ClassC::Function(const B &b) // this is how you declare a parameter of type B&
{
}

您只需要將類型A的對象傳遞給Function 會的 如果您也想采用派生類型,則最好將參數聲明為const 要傳遞this實例,您只需調用:

classCObject.Function(*this);

您是否在語法上遇到麻煩? 它應該是

void ClassC::Function(B& b)
{
    b.DoSomething();
}

使b成為類型B的引用。

暫無
暫無

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

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