是operator = not polimorphic(virtual)? [英] is operator= not polimorphic (virtual)?

查看:68
本文介绍了是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 && ./test
g++ main.cpp -o test
b1 = a: B= called
b1 = b2: A= called
b1.test(a): B::test called
b1.test(b2): B::test called
7of9# g++ --version
g++ (GCC) 3.4.2 [FreeBSD] 20040728
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Is operator= not polymorphic?
Heiner
h.********@nospam.gmx.de
Remove the nospam to get my real address

推荐答案

Heiner写道:
Heiner wrote:
#include< stdio.h>

A级
公开:
虚拟A& operator =(const A&);
虚拟空洞测试(const A&);
};

B类:公共A
{
public:
虚拟A& operator =(const A&);
虚拟空洞测试(const A&);
};
[...]我猜想,b1 = b2调用B :: operator =,因为b1.test(b2)
调用B :: test。但我得到的是:
[...] b1 = a:B =调用
b1 = b2:A =调用
b1.test(a):B ::测试调用
b1.test(b2):B :: test叫
[...]运算符=不是多态的?
#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 &);
}; [...] I would have guessed, that b1 = b2 calls B::operator=, as b1.test(b2)
calls B::test. But what I get is: [...] b1 = a: B= called
b1 = b2: A= called
b1.test(a): B::test called
b1.test(b2): B::test called [...] Is operator= not polymorphic?




是的,但是。 ..

涉及多个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 the
compiler generates one with signature operator=(const B&);
This assignment operator works by assigning base classes and members
individually, thus invoking A::operator=(const A&). This produces the
output you observe.

What exactly do you want to do?

Markus


Heiner写道:


[...]
Heiner wrote:

[...]
int main(int)
{a /;
B b1,b2;
printf(" b1 = a:"); b1 = a;
printf(" b1 = b2:"); b1 = b2;


以上一行调用编译器生成的B& B :: operator =(const B&),其中

依次调用基类的运算符=。

printf(" b1.test(a) :); b1.test(a);
printf(" b1.test(b2):"); b1.test(b2);
返回0;
}


[...]

运算符=不是多态的?
int main (int)
{
A a;
B b1, b2;
printf("b1 = a: "); b1 = a;
printf("b1 = b2: "); b1 = b2;
The above line calls the compiler generated B& B::operator=(const B&), which
in turn calls the base class''s operator=.
printf("b1.test(a): "); b1.test(a);
printf("b1.test(b2): "); b1.test(b2);
return 0;
}
[...]
Is operator= not polymorphic?




如果你把它虚拟化(就像你做的那样)。



It is if you make it virtual (as you did).


"海纳" <无***** @ t-online.de> schrieb im Newsbeitrag

新闻:pa *************************** @ t-online.de ...
"Heiner" <no*****@t-online.de> schrieb im Newsbeitrag
news:pa***************************@t-online.de...

#include< stdio.h>

A级
公开:
虚拟A& operator =(const A&);
虚拟空洞测试(const A&);
};

B类:公共A
{
public:
虚拟A& operator =(const A&);
虚拟空洞测试(const A&);
};

A& A :: operator =(const A& src)
{/> printf(" A = called\\\
);
return * this;
}
void A :: test(const A& src)
{/> printf(A :: test called\\\
);
}

A& B :: operator =(const A& src)
{/> printf(" B = called\\\
);
return * this;
}

void B :: test(const A& src)
{
printf(" B :: test called\\\
);
}

int main(int)
一个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 =,如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 Free Software Foundation,Inc。
这是免费软件;查看复制条件的来源。没有保修;甚至不是为了适销性或特定用途的目的。

运算符=不是多态的?

#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 && ./test
g++ main.cpp -o test
b1 = a: B= called
b1 = b2: A= called
b1.test(a): B::test called
b1.test(b2): B::test called
7of9# g++ --version
g++ (GCC) 3.4.2 [FreeBSD] 20040728
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

Is operator= not polymorphic?




多态性不是在这个例子中的问题。多态性只有在通过指针或对象引用调用
函数时才有意义,而不是直接为已知类型的对象调用它时,




您的猜测是错误的,因为您忘记了编译器提供的分配

运算符,它被称为b1 = b2,而后者又调用了A'的

(显式)赋值运算符,用于打印消息。


HTH

Heinz



Polymorphism doesn''t matter in this example. Polymorphism only matters when
functions are called through a pointer or reference to an object, not when
it is called directly for an object of known type.

Your guess is wrong because you forgot the compiler supplied assignment
operator, which is called for b1 = b2, and which in turn calls A''s
(explicit) assignment operator, which prints the message.

HTH
Heinz


这篇关于是operator = not polimorphic(virtual)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆