本文介绍了是operator = not polimorphic(virtual)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 #include< stdio.h> A级 { 公众: 虚拟A& operator =(const A&); 虚拟空洞测试(const A&); }; class B:公开A { public: 虚拟A& operator =(const A&); 虚拟空洞测试(const A&); }; A &安培; A :: operator =(const A& src) { printf(" A = called\\\); 返回*这个; } void A :: test(const A& src) { printf(A :: test called\\\); } A& B :: operator =(const A& src) { printf(" B = called\\\); 返回*这个; } void B :: test(const A& src) { printf(B :: test called\\\); } int main(int) { A a; B b1,b2; printf(" b1 = a:"); b1 = a; printf(" b1 = b2:"); b1 = b2; printf(" b1.test(a):"); b1.test(a); printf(" b1.test(b2):"); b1.test(b2); 返回0; } 我猜对了,b1 = b2叫B :: operator =,as b1.test(b2) 调用B :: test。但我得到的是: 7of9#gmake&& ./test g ++ main.cpp -o test b1 = a:B =调用 b1 = b2:A =调用 b1.test(a):B ::测试叫 b1.test(b2):B ::测试名为 7of9 #g ++ - -version g ++(GCC)3.4.2 [FreeBSD] 20040728 版权所有(C)2004自由软件基金会,公司 这是免费软件;查看复制条件的来源。没有 保修;甚至没有适销性或适合特定目的。 操作员=不是多态的吗? 海纳 h。******** @ nospam.gmx.de 删除nospam获取我的真实地址 #include <stdio.h> class A{public:virtual A & operator= (const A &);virtual void test(const A &);}; class B : public A{public:virtual A & operator= (const A &);virtual void test(const A &);}; A & A::operator= (const A & src){printf("A= called\n");return * this;} void A::test(const A & src){printf("A::test called\n");} A & B::operator= (const A & src){printf("B= called\n");return * this;} void B::test(const A & src){printf("B::test called\n");} int main (int){A a;B b1, b2;printf("b1 = a: "); b1 = a;printf("b1 = b2: "); b1 = b2;printf("b1.test(a): "); b1.test(a);printf("b1.test(b2): "); b1.test(b2);return 0;} I would have guessed, that b1 = b2 calls B::operator=, as b1.test(b2)calls B::test. But what I get is: 7of9# gmake && ./testg++ main.cpp -o testb1 = a: B= calledb1 = b2: A= calledb1.test(a): B::test calledb1.test(b2): B::test called7of9# g++ --versiong++ (GCC) 3.4.2 [FreeBSD] 20040728Copyright (C) 2004 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Is operator= not polymorphic?Heiner h.********@nospam.gmx.deRemove the nospam to get my real address推荐答案 是的,但是。 .. 涉及多个operator =,这就是你的问题。 你没有为B类提供默认赋值运算符,所以 编译器生成一个带签名运算符=(const B&); 这个赋值运算符的工作原理是分别指定基类和成员 ,从而调用A: :operator =(const A&)。这会产生您观察到的 输出。 您想要做什么? Markus Yes, but...There is more than one operator= involved, and that is your problem.You do not provide a default assignment operator for class B, so thecompiler generates one with signature operator=(const B&);This assignment operator works by assigning base classes and membersindividually, thus invoking A::operator=(const A&). This produces theoutput you observe. What exactly do you want to do? Markus 如果你把它虚拟化(就像你做的那样)。 It is if you make it virtual (as you did). 多态性不是在这个例子中的问题。多态性只有在通过指针或对象引用调用函数时才有意义,而不是直接为已知类型的对象调用它时, 。 您的猜测是错误的,因为您忘记了编译器提供的分配 运算符,它被称为b1 = b2,而后者又调用了A'的 (显式)赋值运算符,用于打印消息。 HTH Heinz Polymorphism doesn''t matter in this example. Polymorphism only matters whenfunctions are called through a pointer or reference to an object, not whenit is called directly for an object of known type. Your guess is wrong because you forgot the compiler supplied assignmentoperator, which is called for b1 = b2, and which in turn calls A''s(explicit) assignment operator, which prints the message. HTHHeinz 这篇关于是operator = not polimorphic(virtual)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 08:06