久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么我不能對普通變量進行多態?

Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
本文介紹了為什么我不能對普通變量進行多態?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我是一名 Java 程序員,最近開始學習 C++.我被某些事情搞糊涂了.

I'm a Java programmer and recently started studying C++. I'm confused by something.

我知道在 C++ 中,要實現多態行為,您必須使用指針或引用.例如,考慮一個帶有實現方法 getArea()Shape 類.它有幾個子類,每個子類都以不同的方式覆蓋 getArea().比考慮以下功能:

I understand that in C++, to achieve polymorphic behavior you have to use either pointers or references. For example, consider a class Shape with an implemented method getArea(). It has several subclasses, each overriding getArea() differently. Than consider the following function:

void printArea(Shape* shape){
    cout << shape->getArea();
}

函數根據指針指向的具體Shape調用正確的getArea()實現.

The function calls the correct getArea() implementation, based on the concrete Shape the pointer points to.

效果相同:

void printArea(Shape& shape){
    cout << shape.getArea();
}

但是,以下方法不能多態地工作:

However, the following method does not work polymorphicaly:

void printArea(Shape shape){
    cout << shape.getArea();
}

不管函數中傳入什么樣的Shape,都會調用相同的getArea()實現:Shape<中的默認實現/代碼>.

Doesn't matter what concrete kind of Shape is passed in the function, the same getArea() implementation is called: the default one in Shape.

我想了解這背后的技術推理.為什么多態適用于指針和引用,而不適用于普通變量?(而且我想這不僅適用于函數參數,而且適用于任何東西.

I want to understand the technical reasoning behind this. Why does polymorphism work with pointers and references, but not with normal variables? (And I suppose this is true not only for function parameters, but for anything).

請解釋這種行為的技術原因,以幫助我理解.

Please explain the technical reasons for this behavior, to help me understand.

推薦答案

答案是復制語義.

當您在 C++ 中按值傳遞對象時,例如printArea(Shape shape) 一個副本是你傳遞的對象.如果將派生類傳遞給該函數,則復制的只是基類 Shape.仔細想想,編譯器不可能做任何其他事情.

When you pass an object by value in C++, e.g. printArea(Shape shape) a copy is made of the object you pass. And if you pass a derived class to this function, all that's copied is the base class Shape. If you think about it, there's no way the compiler could do anything else.

Shape shapeCopy = circle;

shapeCopy 被聲明為 Shape,而不是 Circle,所以編譯器所能做的就是構造一個 的副本對象的形狀部分.

shapeCopy was declared as a Shape, not a Circle, so all the compiler can do is construct a copy of the Shape part of the object.

這篇關于為什么我不能對普通變量進行多態?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
Is pointer comparison undefined or unspecified behavior in C++?(C++ 中的指針比較是未定義或未指定的行為嗎?)
主站蜘蛛池模板: 久久99国产精一区二区三区 | 国产精品久久久久久久久久久久 | 精品欧美一区二区三区精品久久 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 成人免费看片网 | 国产精品久久久久久婷婷天堂 | 色婷婷一区二区三区四区 | 欧美精品中文字幕久久二区 | 7777精品伊人久久精品影视 | 亚洲精品1区 | 夜夜爽99久久国产综合精品女不卡 | 一区二区伦理电影 | 亚洲精品9999 | 天天综合操 | 武道仙尊动漫在线观看 | 激情 亚洲 | 国产午夜精品久久久久免费视高清 | 久久午夜电影 | 91麻豆精品国产91久久久久久 | 国产日韩精品视频 | 在线国产一区二区三区 | 久久精品中文 | 3级毛片 | 亚洲精品乱码久久久久久9色 | 亚洲综合网站 | 亚洲欧美一区二区三区在线 | 欧美在线视频一区二区 | 在线观看av网站 | 欧美成人免费在线视频 | 精品国产欧美一区二区三区成人 | 波多野结衣二区 | 欧美成人精品二区三区99精品 | 久久久久黄 | 久久久亚洲精品视频 | xxxxxx国产 | 日韩aⅴ在线观看 | 国产精品免费一区二区三区四区 | 国产日韩免费观看 | 久久国产一区二区 | 91网在线播放 | 欧美日韩国产在线观看 |