它看起来像糟糕的编程,但是呢? [英] It looks like bad programming, but is it?

查看:51
本文介绍了它看起来像糟糕的编程,但是呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个大类中执行此操作以避免重写复制ctr代码。它好像b $ b似乎工作正常,但我想知道我是否可能会在后面拍摄自己

...我不能认为它有任何问题,但它b ​​/ b $ b看起来很糟糕的编程。 :)


class MyClass

{

public:

//...


//赋值运算符定义

MyClass& operator =(const MyClass& copy)

{

if(this!=& copy)

{

// ...复制...

}

}


//复制构造函数

MyClass(const MyClass& copy)

{

*(const_cast< MyClass *>(this))= copy;

}


// ...

};

I do this in several large classes to avoid rewriting copy ctr code. It
seems to work okay, but I was wondering if I might be shooting myself
in the foot later... I can''t think of anything wrong with it, but it
looks like bad programming. :)

class MyClass
{
public:
//...

//Assignment operator defined
MyClass& operator=(const MyClass& copy)
{
if(this != &copy)
{
//... do the copy...
}
}

//Copy constructor
MyClass(const MyClass& copy)
{
*(const_cast<MyClass*>(this)) = copy;
}

//...
};

推荐答案

John Fullman写道:
John Fullman wrote:
我在几个大类中这样做,以避免重写复制ctr代码。它似乎工作正常,但我想知道我是否可能在以后拍摄自己
...我不能想到它有什么问题,但它看起来像
喜欢糟糕的节目。 :)

类MyClass
{
公开:
// ...

//赋值运算符定义
MyClass& ; operator =(const MyClass& copy)
{
if(this!=& copy)
//
// ...复制......
}

//复制构造函数
MyClass(const MyClass& copy)
{
*(const_cast< MyClass *>(this)) =复制;


// ...
};
I do this in several large classes to avoid rewriting copy ctr code. It
seems to work okay, but I was wondering if I might be shooting myself
in the foot later... I can''t think of anything wrong with it, but it
looks like bad programming. :)

class MyClass
{
public:
//...

//Assignment operator defined
MyClass& operator=(const MyClass& copy)
{
if(this != &copy)
{
//... do the copy...
}
}

//Copy constructor
MyClass(const MyClass& copy)
{
*(const_cast<MyClass*>(this)) = copy;
}

//...
};




怎么样


class MyClass

{

private:

CopyFrom(const MyClass& src );

public:

MyClass& operator =(const MyClass& copy)

{

if(this!=& copy)

{

CopyFrom(copy);

}

}


MyClass(const MyClass& copy)

{

CopyFrom(copy);

}



How about:

class MyClass
{
private:
CopyFrom(const MyClass& src);
public:
MyClass& operator=(const MyClass& copy)
{
if (this != &copy)
{
CopyFrom(copy);
}
}

MyClass(const MyClass& copy)
{
CopyFrom(copy);
}


" John Fullman" <乔********** @ gmail.com>写在

新闻:11 ******************** @ g44g2000cwa.googlegrou ps.com:
"John Fullman" <jo**********@gmail.com> wrote in
news:11********************@g44g2000cwa.googlegrou ps.com:
我在几个大类中执行此操作以避免重写复制ctr代码。它似乎工作正常,但我想知道我是否可能在以后拍摄自己
...我不能想到它有什么问题,但它看起来像
喜欢糟糕的节目。 :)

类MyClass
{
公开:
// ...

//赋值运算符定义
MyClass& ; operator =(const MyClass& copy)
{
if(this!=& copy)
//
// ...复制......
}

//复制构造函数
MyClass(const MyClass& copy)
{
*(const_cast< MyClass *>(this)) =复制;
}

// ...
};
I do this in several large classes to avoid rewriting copy ctr code. It
seems to work okay, but I was wondering if I might be shooting myself
in the foot later... I can''t think of anything wrong with it, but it
looks like bad programming. :)

class MyClass
{
public:
//...

//Assignment operator defined
MyClass& operator=(const MyClass& copy)
{
if(this != &copy)
{
//... do the copy...
}
}

//Copy constructor
MyClass(const MyClass& copy)
{
*(const_cast<MyClass*>(this)) = copy;
}

//...
};




取决于你的课程。但根据经验,可能不是最好的b / b
1)当你开始时,复制构造函数会做_bad_事情/>
继承东西。 (它骰子!它切片!.....)


2)你支付默认构建你的班级的费用(以及所有成员

变量,和父类),然后扔掉所有的工作,重新/

几乎立即分配大部分(全部?)的值。


3a)如何你经常做自我指派吗?如果你从未实际自行分配,为什么要支付比较费用?


3b)你的作业代码是否真的对自我分配敏感?

如果你的作业代码做正确的事情,即使是自我分配,

那么你不需要做比较而只是自我分配。 (另外

考虑异常安全问题...)


4)const_cast是一个直接的红旗,表示出错了......(我不认为这是复制构造函数中的常量......为什么那么

const_cast呢?)



Depends on your class. But as a rule of thumb, probably not the
greatest:

1) That copy constructor''s gonna do _bad_ things when you start to
inherit stuff. (It dices! It slices!.....)

2) You pay the cost of default constructing your class (and all member
variables, and parent classes), then throw away all of that work by re-
assigning most (all?) of the values almost immediately.

3a) How often do you actually perform a self-assignment? Why pay the
cost of the comparision if you never actually self-assign.

3b) Is your assignment code actually that sensitive to self-assignment?
If your assignment code does the right thing, even for self-assignment,
then you don''t need to do the compare and just self-assign. (Also
consider exception safety concerns...)

4) That const_cast is an immediate red flag that something''s wrong.... (I
didn''t think this was const in a copy constructor... why is that
const_cast there at all?)


John Fullman写道:
John Fullman wrote:
我在几个大类中执行此操作以避免重写复制ctr代码。它似乎工作正常,但我想知道我是否可能在以后拍摄自己
...我不能想到它有什么问题,但它看起来像
喜欢糟糕的节目。 :)
I do this in several large classes to avoid rewriting copy ctr code. It
seems to work okay, but I was wondering if I might be shooting myself
in the foot later... I can''t think of anything wrong with it, but it
looks like bad programming. :)




推荐的方法是根据

复制构造函数编写赋值运算符,而不是相反。


class X

{

public:

X(const X& rhs);

无效掉期(X& rhs);

X& operator =(const X& rhs)

{

X tmp(rhs);

swap(tmp);

返回* this;

}

};


这样做的好处是异常安全,同时避免代码

重复。交换通常很容易编写(并且它自己有用

吧)。


但最好的方法是尽可能写类其中默认的
复制构造函数和赋值运算符是正确的,例如除非你有充分的理由,否则请使用指针来避免



john



The recommended way is to write the assignment operator in terms of the
copy constructor, not the other way around.

class X
{
public:
X(const X& rhs);
void swap(X& rhs);
X& operator=(const X& rhs)
{
X tmp(rhs);
swap(tmp);
return *this;
}
};

This has the advantage of being exception safe, as well as avoiding code
duplication. Swap is usually very easy to write (and useful in it''s own
right).

But the best way is as far possible to write classes where the default
copy constructor and assignment operator do the right thing, e.g. avoid
using pointers unless you have very good reason.

john


这篇关于它看起来像糟糕的编程,但是呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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