简体   繁体   English

如何避免在转换函数中使用复制构造函数?

[英]How do I avoid copy constructor in the conversion function?

I am trying to convert between two classes and avoid a temporary object. 我试图在两个类之间进行转换,并避免使用临时对象。

Here is my declaration for Square : 这是我对Square声明:

class CSquare
{
public:
    int dimension;
    CSquare(int dimension);

    // Conversion to Rectangle
    operator CRectangle();

    ~CSquare(void);
};

and here is my declaration for Rectangle : 这是我对Rectangle声明:

class CRectangle
{
public:
    int length;
    int width;

    CRectangle(int length, int width);

    //Copy Constructor
    CRectangle(const CRectangle& anotherRectangle); 

    ~CRectangle(void);
};

Why does 为什么

CSquare aSquare = CSquare(10);
    CRectangle convertedRect = (CRectangle) aSquare;

invoke the copy constructor? 调用副本构造函数?

I have a conversion function: 我有一个转换功能:

CSquare::operator CRectangle(){
    return CRectangle(CSquare::dimension,CSquare::dimension);
}

but I'm still getting a temporary object. 但我仍然得到一个临时对象。

How do I avoid the temporary object? 如何避免临时对象?

You can avoid copy construction by writing a conversion constructor in CRectangle: 您可以通过在CRectangle中编写转换构造函数来避免复制构造:

CRectangle::CRectangle(const CSquare & sq)
    :length(sq.dimension),
     width(sq.dimension)
{}
...
...
CSquare aSquare(10);
CRectangle convertedRect(aSquare);

Because you're copying a Rectangle. 因为您要复制矩形。

I honestly don't know what else to say. 老实说,我不知道该说些什么。 Compilers are free to optimize away the call in the case you're showing, but they don't have to. 编译器可以在您显示的情况下自由优化调用,但并非必须如此。 Apparently yours is not. 显然你不是。

Edit.... 编辑....

As to how to avoid it, you really can't. 至于如何避免它,您确实不能。 You could try to crank up the optimizations but it can be extremely difficult to see if you've succeeded, especially with such a small, trivial construct. 您可以尝试加快优化速度,但是要想知道成功与否却非常困难,尤其是对于这么小的琐碎结构而言。 If I where you I wouldn't be so worried about it unless I really found, by profiling my code, that an inordinate amount of time was being spent in that function. 如果我在您的身边,除非我真的通过对代码进行性能分析发现确实花了过多的时间在该函数上,否则我不会为此担心。 Then I'd be looking for ways to crunch the algorithms using the code to reduce copies. 然后,我将寻找使用代码减少副本的算法来简化算法。

How do I avoid the temporary object? 如何避免临时对象?

You don't. 你不知道 That's how conversions work. 这就是转换的工作方式。 The temporary may be elided (constructing directly into the destination), but that is not required. 可以省去临时项(直接构造到目标中),但这不是必需的。

It sounds like you want move semantics, which are in C++0x. 听起来您想使用C ++ 0x中的移动语义。 Even then, the temporary "exists", but it can be moved instead of copied, which, for types where it matters, is often much more efficient. 即使这样,临时的“存在”也可以移动而不是复制,这对于重要的类型通常效率更高。

Is this square/rectangle case not really the situation you care about and only used here for an example? 这个方形/矩形的情况不是您真正关心的情况,仅在这里用作示例吗?

正在创建一个临时CRectangle对象,然后将其复制到convertRect。

我会假设,因为operator CRectangle返回的实例CRectangle ,拷贝构造函数被调用的返回值。

In general this depends on the compiler. 通常,这取决于编译器。 Return value optimization is done by most compiler. 返回值优化由大多数编译器完成。 So instead of creating a temporary object and assign it to your object the copy Ctor from your object is called. 因此,不是创建临时对象并将其分配给对象,而是调用对象的副本Ctor。

Also take a look here Copy constructor vs. return value optimization 在这里也可以看看复制构造函数与返回值优化

I can't tell which type of object you're saying has a temporary. 我无法说出您说的是哪种类型的物体具有临时物体。

If the square, then you need to remove any excuse for the compiler to create one: 如果是正方形,那么您需要删除编译器创建一个借口:

CSquare aSquare(10);

If the rectangle, then you shoudl use a converting constructor instead of an operator, as suggested by @PigBen: 如果是矩形,则应使用转换构造函数而不是@PigBen建议的运算符:

Rectangle::CRectangle(const CSquare & sq)
    :length(sq.dimension),
     width(sq.dimension)
{}
// And then:
CRectangle convertedRect(aSquare);

In this particular case, you do it by not having the conversion function and instead use inheritance. 在这种特殊情况下,您可以通过不具有转换功能而使用继承来实现。 Eg: 例如:

class CRectangle
{
public:
    int length;
    int width;

    CRectangle(int length, int width);
};

class CSquare : public CRectangle
{
public:
    CSquare(int dimension) : length(dimension), width(dimension)
    {}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免转换运算符调用复制构造函数? - How do I avoid a conversion operator to invoke the copy constructor? 如何避免使用插入迭代器调用复制构造函数 - How do I avoid invoking the copy constructor with insertion iterators 如何避免通用功能模板中的多余副本? - How do I avoid the extra copy in the generic function template? 如何使用带指针的转换构造函数? - How do I use a conversion constructor with pointers? 如何获得通过可变参数构造函数调用的复制构造函数? - How do I get the copy constructor called over a variadic constructor? C ++(逻辑复制构造函数)如何复制对象? - C++ (Logical Copy Constructor) How do I copy an object? 我需要复制构造函数吗? 以及如何使用复制构造函数将const对象复制到非const对象 - do i need copy constructor ? and how copy const object to non const object using copy constructor 如何在lambda函数中避免隐式移动构造函数 - How Can I avoid implicit move constructor inside a lambda function 您如何在类的成员函数中调用复制构造函数? - How do you call the copy constructor within a member function of a class? 避免向量复制构造函数 - avoid vector copy constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM