2016-12-26 33 views
0

我已经写了一个点结构我正在使用来模拟正文问题。我发现很难完全理解和实施交换成语,并使其适应我的需求,这主要是速度。我是否正确地做这件事?在C++ 17中会有所不同吗?C++&交换/复制应用到一个点结构

#pragma once 
#include <algorithm> 

struct Point 
{ 
    double x, y, z; 

    explicit Point(double X = 0, double Y = 0, double Z = 0) : x(X), y(Y), z(Z) {} 
    void swap(Point&, Point&); 

    inline bool operator==(Point b) const { return (x == b.x && y == b.y && z == b.z); } 
    inline bool operator!=(Point b) const { return (x != b.x || y != b.y || z != b.z); } 

    Point& operator=(Point&); 
    Point& operator+(Point&) const; 
    Point& operator-(Point&) const; 
    inline double operator*(Point& b) const { return b.x*x + b.y*y + b.z*z; } // Dot product 
    Point& operator%(Point&) const; // % = Cross product 

    inline Point& operator+=(Point& b) { return *this = *this + b; } 
    inline Point& operator-=(Point& b) { return *this = *this - b; } 
    inline Point& operator%=(Point& b) { return *this = *this % b; } 

    Point& operator*(double) const; 
    Point& operator/(double) const; 

    inline Point& operator*=(double k) { return *this = *this * k; } 
    inline Point& operator/=(double k) { return *this = *this/k; } 
}; 

std::ostream &operator<<(std::ostream &os, const Point& a) { 
    os << "(" << a.x << ", " << a.y << ", " << a.z << ")"; 
    return os; 
} 

void Point::swap(Point& a, Point& b) { 
    std::swap(a.x, b.x); 
    std::swap(a.y, b.y); 
    std::swap(a.z, b.z); 
} 

Point& Point::operator=(Point& b) { 
    swap(*this, b); 
    return *this; 
} 

Point& Point::operator+(Point& b) const { 
    Point *p = new Point(x + b.x, y + b.y, z + b.z); 
    return *p; 
} 

Point& Point::operator-(Point& b) const { 
    Point *p = new Point(x - b.x, y - b.y, z - b.z); 
    return *p; 
} 

Point& Point::operator%(Point& b) const { 
    Point *p = new Point(
     y*b.z - z*b.y, 
     z*b.x - x*b.z, 
     x*b.y - y*b.x 
); 

    return *p; 
} 

Point& Point::operator*(double k) const { 
    Point *p = new Point(k*x, k*y, k*z); 
    return *p; 
} 

Point& Point::operator/(double k) const { 
    Point *p = new Point(x/k, y/k, z/k); 
    return *p; 
} 
+5

看起来像你的代码像筛子一样泄漏内存。 –

+0

您需要从代码中删除所有指针和所有对'new'的调用。还要更改所有正常的算术运算符(+,不是+ =)以按值返回。 –

+2

您'operator ='函数更改赋值运算符的* both *两边的对象。它也不能用于运算符右侧的右值(类似于“右值”中的“r”)。如果你使用交换,操作员应该通过值*来取其参数*,如果不是,则通过*常数*参考取其参数*。 –

回答

4

复制/交换,ideom实际拷贝swap() S中的值。你的“改编”仅仅是swap()s。正确的使用复制/交换-ideom会看,比如,像这样:

Point& Point::operator= (Point other) { // note: by value, i.e., already copied 
    this->swap(other); 
    return *this; 
} 

(当然,这也假定您的swap()功能是考虑只是一个额外的参数成员:有已经与之交换的对象)。

如果速度是您最关心的问题,那么复制/交换代码可能不适用于Point的情况:复制操作本质上是微不足道的。与相对涉及的操作相比,交换值是相当合理的,例如将旧数组复制一个std::vector,其中除了复制可能的多个值和一些分配操作之外,交换操作仅仅指向几个指针交换。也就是说,你的Point分配可能是最好的关闭只是分配所有成员:

Point& Point::operator= (Point const& other) { // note: no copy... 
    this->x = other.x; 
    this->y = other.y; 
    this->z = other.z; 
    return *this; 
} 

正如指出的评论,你也应该分配新Point对象与new:C++是不是Java或C# !你可以在堆栈上创建一个物体,它不需要来自堆,例如:

Point Point::operator+ (Point const& other) const { 
    return Point(this->x + other.x, this->y + other.y, this->z + other.z); 
} 
+0

感谢您的详细解答并感谢其他意见。我相应地编辑了我的代码。 – Sebastian

相关问题