简体   繁体   English

Class 构造函数 arguments C++

[英]Class Constructor arguments C++

I am creating a pair1 class for ax and y Cartesian coordinate system.我正在为 ax 和 y 笛卡尔坐标系创建一对 class。 x and y are doubles. x 和 y 是双精度数。 I need to have 3 constructors.我需要有 3 个构造函数。

  1. No arguments, defaults x and y to zero.没有 arguments,默认 x 和 y 为零。
  2. One arguement assigns x and defaults y to zero.一种论点将 x 赋值并将 y 默认为零。
  3. One arugeument defaults x to zero and assigns y.一种参数默认 x 为零并分配 y。 I'm not sure if I am setting up the class right.我不确定我是否正确设置了 class。 I get the follwing error: pair1::pair1(double) and pair1::pair1(double) cannot be overloaded.我收到以下错误: pair1::pair1(double)pair1::pair1(double)不能重载。

My class:我的 class:

class pair1
{
private:
    double x;
    double y;

public:
    pair1(){ x = 0.0, y = 0.0; }    
    pair1( double a ){ x = a; y =0.0; }
    pair1(double b){ x = 0.0;  y = b; }
};

1) no arguments, defaults x and y to zero. 1) 没有 arguments,默认 x 和 y 为零。

That's easy这很容易

2) one arguement assigns x and defaults y to zero. 2) 一个论点指定 x 并将 y 默认为零。

3) one arugeument defaults x to zero and assigns y. 3)一个arugeument默认x为零并分配y。

That's a problem.那是个问题。 How do you know, when you only have one parameter, which of the two is meant to be called?当您只有一个参数时,您怎么知道要调用这两个参数中的哪一个? That's why you get a compilation error.这就是你得到编译错误的原因。

Instead - use the default constructor (the one with no parameters), full constructor (the one with both), if needed, and SetX() and SetY() to set the X and Y separately, and make distinction by the name of the function.取而代之 - 使用默认构造函数(没有参数的那个)、完整的构造函数(两个都有),如果需要,使用SetX()SetY()分别设置 X 和 Y,并通过名称来区分function。

class pair1
{
    private:
    double x;
    double y;

    public:
    pair1( double a=0.0, double b=0.0 ){ x = a; y =b; };
                     // default value 0.0 allows to only
                     // set x, and leave y to be the default,
                     // or leave them both default.
    void SetX(double a) { x=a;};
    void SetY(double b) { y=b;};
};

The problem is that the compiler has no way to distinguish问题是编译器没有办法区分

pair1(double a)

and

pair1(double b)

Indeed, they are the same thing except for the name of the parameter.实际上,除了参数的名称之外,它们是相同的。 For example:例如:

pair1 myPair1(123.456); // Which constructor should be called?

This is called ambiguous overloading.这称为模糊重载。

pair1( double a ){ x = a; y =0.0; }
pair1(double b){ x = 0.0;  y = b; }

These are exactly same constructor.这些是完全相同的构造函数。 Different parameter name doesn't make any difference.不同的参数名称没有任何区别。 All that matters for overloading is, the type(s) and number of types, and their ordering .重载重要的是类型和类型的数量,以及它们的顺序

I'm not sure that having default arguments except for the (0,0) case is of any use, but something like this could work:我不确定除了 (0,0) 情况外,默认 arguments 是否有任何用途,但这样的事情是否可行:

struct X
{
    double value;
    explicit X(double v) : value(v) {}
};

struct Y
{
    double value;
    explicit Y(double v) : value(v) {}
};

class pair1
{
    private:
        double x;
        double y;

    public:
        pair1() : x(0.0), y(0.0) {}
        explicit pair1(X a) : x(a.value), y(0.0) {}
        explicit pair1(Y b) : x(0.0), y(b.value) {}
        pair1(X a, Y b) : x(a.value), y(b.value) {}  // For completeness
        pair1(Y b, X a) : x(a.value), y(b.value) {}  // For super-completeness
        pair1(double a, double b) : x(a), y(b) {}
};

Use:利用:

pair1 aPair(X(2.0));                 // default y
pair1 anotherPair(Y(4.875));         // default x
pair1 aThirdPair(X(1.0), Y(1.0));
pair1 aForthPair(Y(100.0), X(1.0));  // keyword arguments ;-)
pair1 quintus(23.0, 45.6);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM