第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如果我要重寫基類的虛函數(shù),可以調(diào)用它嗎?

如果我要重寫基類的虛函數(shù),可以調(diào)用它嗎?

C++
牧羊人nacy 2019-11-22 15:37:20
假設(shè)我有課程,F(xiàn)oo并且Bar設(shè)置如下:class Foo{public:    int x;    virtual void printStuff()    {        std::cout << x << std::endl;    }};class Bar : public Foo{public:    int y;    void printStuff()    {        // I would like to call Foo.printStuff() here...        std::cout << y << std::endl;    }};如代碼中所注釋的那樣,我希望能夠調(diào)用我要重寫的基類的函數(shù)。在Java中有super.funcname()語法。這在C ++中可能嗎?
查看完整描述

3 回答

?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

C ++語法如下:


class Bar : public Foo {

  // ...


  void printStuff() {

    Foo::printStuff(); // calls base class' function

  }

};


查看完整回答
反對(duì) 回復(fù) 2019-11-22
?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

是,


class Bar : public Foo

{

    ...


    void printStuff()

    {

        Foo::printStuff();

    }

};

它與superJava中的相同,不同之處在于它允許您在具有多個(gè)繼承時(shí)從不同的基礎(chǔ)調(diào)用實(shí)現(xiàn)。


class Foo {

public:

    virtual void foo() {

        ...

    }

};


class Baz {

public:

    virtual void foo() {

        ...

    }

};


class Bar : public Foo, public Baz {

public:

    virtual void foo() {

        // Choose one, or even call both if you need to.

        Foo::foo();

        Baz::foo();

    }

};


查看完整回答
反對(duì) 回復(fù) 2019-11-22
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

有時(shí),當(dāng)您不在派生函數(shù)中時(shí),您需要調(diào)用基類的實(shí)現(xiàn)...它仍然有效:


struct Base

{

    virtual int Foo()

    {

        return -1;

    }

};


struct Derived : public Base

{

    virtual int Foo()

    {

        return -2;

    }

};


int main(int argc, char* argv[])

{

    Base *x = new Derived;


    ASSERT(-2 == x->Foo());


    //syntax is trippy but it works

    ASSERT(-1 == x->Base::Foo());


    return 0;

}


查看完整回答
反對(duì) 回復(fù) 2019-11-22
  • 3 回答
  • 0 關(guān)注
  • 546 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)