Protocol类中的Virtual Assignment Operator [英] Virtual Assignment Operator in Protocol class

查看:53
本文介绍了Protocol类中的Virtual Assignment Operator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

假设我有一个Protocol类,我还需要一个赋值

operator =()。

class B

{

.....

虚拟B& operator =(const B& rb)= 0;

};

现在在一些派生类D:public B中,我将如何继续

operator =?我是否需要提供2名操作员:

-01以覆盖D& operator =(const B& d)

-和01重载D& operator =(const D& d)?

=(im)purality如何=?

感谢您的指导。

Dear,
Suppose I have a Protocol class, in which I need also an assignment
operator =().
class B
{
.....
virtual B& operator=(const B& rb) =0;
};
Now in some derived class D: public B, how would I proceed with
operator=? Do I need to supply 2 operators:
-01 to override D& operator=(const B& d)
-and 01 to overload D& operator=(const D& d)?
How about the (im)purality of = ?
Thanks for your guidance.

推荐答案

N4M写道:

亲爱的,
假设我有一个Protocol类,其中我还需要一个作业
operator =()。
B级
{
....
虚拟B& operator =(const B& rb)= 0;
};
现在在一些派生类D:public B中,我将如何继续运行
operator =?我是否需要提供2个操作员:
-01以覆盖D& operator =(const B& d)
- 和01重载D& operator =(const D& d)?
如何(im)purality =?
感谢您的指导。

Dear,
Suppose I have a Protocol class, in which I need also an assignment
operator =().
class B
{
....
virtual B& operator=(const B& rb) =0;
};
Now in some derived class D: public B, how would I proceed with
operator=? Do I need to supply 2 operators:
-01 to override D& operator=(const B& d)
-and 01 to overload D& operator=(const D& d)?
How about the (im)purality of = ?
Thanks for your guidance.




简而言之:op = as virtual很少是一个好主意。大多数情况下,因为大多数人都不希望它工作,所以它不起作用。

提示:多态性只能通过检查函数来实现
$ b调用$ b,但不考虑参数的运行时类型




假设类D,派生自B类


B * p1 =新D;

B * p2 =新D;


* p1 = * p2 ;


在上面,你认为编译器在寻找一个函数来满足
$ b的请求时,你在寻找

$ b作业? :


1)B :: operator =(const B& Arg);

或2)B :: operator =(const D& Arg);

或3)D :: operator =(const B& Arg);

或4)D :: operator =(const D& Arg);


-

Karl Heinz Buchegger
kb **** **@gascad.at


Karl Heinz Buchegger写道:
Karl Heinz Buchegger wrote:

N4M写道:

N4M wrote:

亲爱的,
假设我有一个Protocol类,我还需要一个赋值
operator =()。
class B
{
....
虚拟B& operator =(const B& rb)= 0;
};
现在在一些派生类D:public B中,我将如何继续运行
operator =?我是否需要提供2个操作员:
-01以覆盖D& operator =(const B& d)
- 和01重载D& operator =(const D& d)?
如何(im)purality =?
感谢您的指导。
简而言之:op = as virtual很少是一个好主意。大多数情况下因为大多数人都没想到它会起作用。
提示:多态性只能通过检查函数
被调用的对象来起作用,但是不需要将参数的运行时类型考虑在内。

Dear,
Suppose I have a Protocol class, in which I need also an assignment
operator =().
class B
{
....
virtual B& operator=(const B& rb) =0;
};
Now in some derived class D: public B, how would I proceed with
operator=? Do I need to supply 2 operators:
-01 to override D& operator=(const B& d)
-and 01 to overload D& operator=(const D& d)?
How about the (im)purality of = ?
Thanks for your guidance.
In a nutshell: having op= as virtual is seldome a good idea. Mostly
because it doesn''t work how most people would expect it to work.
Hint: polymorphism works only by examining the object the function
is called for, but doesn''t take the runtime type of the arguments
into account.




重新思考(并抽烟)后我不认为这是

不再相关。请继续阅读...

假设D级,来自B级

B * p1 =新D;
B * p2 =新D;
* p1 = * p2;

在上面,你认为编译器在寻找一个函数来满足
的要求时会怎么样?分配? :

1)B :: operator =(const B& Arg);
或2)B :: operator =(const D& Arg);
或3)D :: operator =(const B& Arg);
或4)D :: operator =(const D& Arg);



After rethinking (and smoking a cigarette) I don''t think this to be
relevant any more. Read on ...

Assume class D, derived from class B

B* p1 = new D;
B* p2 = new D;

*p1 = *p2;

In the above, what do you think the compiler is looking for
when searching a function to fullfil the request of the
assignment? :

1) B::operator=( const B& Arg );
or 2) B::operator=( const D& Arg );
or 3) D::operator=( const B& Arg );
or 4) D::operator=( const D& Arg );




下一步思考实验(你可能想用你的编译器尝试这个

无论如何):让操作员虚拟。

然后调用哪个op =?


提示:想想当一个对象的运行时类型与指向

的指针的静态类型不同时取消引用操作的作用。


-

Karl Heinz Buchegger
kb **** **@gascad.at




" Karl Heinz Buchegger" < KB ****** @ gascad.at>在消息中写道

news:41 *************** @ gascad.at ...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...

在一个简而言之:op = as virtual很少是一个好主意。大多数情况下因为大多数人都没想到它会起作用。
提示:多态性只能通过检查函数
被调用的对象来起作用,但是不需要考虑运行时类型的参数



那么程序员必须这样做,不是吗?

假设D级,来自B级

B * p1 = new D ;
B * p2 =新D;

* p1 = * p2;

在上面,您认为编译器正在寻找什么
在搜索某个函数以满足
分配的请求时? :

1)B :: operator =(const B& Arg);
或2)B :: operator =(const D& Arg);
或3)D :: operator =(const B& Arg);
或4)D :: operator =(const D& Arg);


由于虚拟作业的原因,你所展示的症状不存在(也不会变得更好

或更差),但是因为如何
C ++对象模型有效。在现实世界中,虚拟任务不是疾病,而是治愈!


考虑这个例子:


// ========================================

struct B

{

//这是一个非常规范的课程,除了虚拟作业


虚拟B& operator =(const B&);


//派生删除所需的虚拟析构函数*


virtual~B ();


//添加更多成员(数据或功能)以适应口味...

};

struct D:public B

{

int value;


D(int i = 0):value(i)< br $>
{

}

D(常数D& r):B(r),值(r.value)< br $>
{

}

虚拟D& operator =(const D& r)

{

B :: operator =(r);

value = r.value;

return * this;

}


虚拟B& operator = (const B& r)

{

const D * p = dynamic_cast< const D *>(& r);


if(p)

{

* this = * p;

}

else

{

// D被切片,但现在你可以检测并补偿!!!

B :: operator =(r);

值= 0;

}


返回* this;

}

};


// ============================== ==========


现在你的例子......

B * p1 =新D;
B * p2 =新D;

* p1 = * p2;


....将分配as-if使用最直观的选择...

1)B :: operator =(const B& Arg) ;
或2)B :: operator =(const D& Arg);
或3)D :: operator =(const B& Arg);
或4)D :: operator =(const D& Arg);

In a nutshell: having op= as virtual is seldome a good idea. Mostly
because it doesn''t work how most people would expect it to work.
Hint: polymorphism works only by examining the object the function
is called for, but doesn''t take the runtime type of the arguments
into account.
Then programmer must do that, no?
Assume class D, derived from class B

B* p1 = new D;
B* p2 = new D;

*p1 = *p2;

In the above, what do you think the compiler is looking for
when searching a function to fullfil the request of the
assignment? :

1) B::operator=( const B& Arg );
or 2) B::operator=( const D& Arg );
or 3) D::operator=( const B& Arg );
or 4) D::operator=( const D& Arg );
The symptoms you demonstrate do not exist (nor become better
or worse) because of virtual assignment, but because of how
C++ object model works. In real world, virtual assignment is not
the disease, but the cure!

Consider this example:

// ========================================

struct B
{
// this is a pretty canonical class, except for virtual assignment

virtual B &operator=( const B & );

// virtual destructor needed for derived deletion thru base *

virtual ~B();

// add more members (data or functions) to suit the taste...
};
struct D : public B
{
int value;

D( int i=0 ) : value(i)
{
}

D( const D &r ) : B(r) , value(r.value)
{
}

virtual D &operator=( const D &r )
{
B::operator=( r );
value = r.value;
return *this;
}

virtual B &operator=( const B &r )
{
const D *p = dynamic_cast<const D *>(&r);

if( p )
{
*this = *p;
}
else
{
// D is being sliced, but now you can detect and compensate!!!
B::operator=( r );
value = 0;
}

return *this;
}
};

// ========================================

Now your example...
B* p1 = new D;
B* p2 = new D;

*p1 = *p2;
.... will assign as-if using the most intuitive choice...
1) B::operator=( const B& Arg );
or 2) B::operator=( const D& Arg );
or 3) D::operator=( const B& Arg );
or 4) D::operator=( const D& Arg );




....当然是4。


- Risto -



.... which, of course, is 4 .

- Risto -


这篇关于Protocol类中的Virtual Assignment Operator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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