简体   繁体   English

如何从另一个类参数访问类构造函数参数

[英]How to access a class constructor parameter from the another class parameter

STRUGGLING WITH C++ CONSTRUCTOR ARGUMENTS与 C++ 构造函数参数斗争

So, I've just came from TS/JS/Py and trying to understand C++ concepts.所以,我刚从 TS/JS/Py 过来,并试图理解 C++ 概念。 But I'm struggling with using the parameter of constructor of the class FOR declaring default value for an argument.但是我正在努力使用类的构造函数的参数来声明参数的默认值。 Here is the code I'm trying to run:这是我试图运行的代码:

double Phythagorean_Hypotenuse (int& a, int& b ) {
    return sqrt((a * a) + (b * b));
};

class Triangle {
    public:
      int a;
      int b;
      double c;
      Triangle(int a_param, int b_param, double c_param = Phythagorean_Hypotenuse(a_param, b_param)) {
            a = a_param;
            b = b_param;
            c = c_param;
      }
};

and inside of the main function和主函数内部

Triangle mytri_1(10, 20);
std::cout << mytri_1.a << std:endl;

But when I try to run this code, IDE is throwing me some errors like但是当我尝试运行此代码时,IDE 向我抛出了一些错误,例如

[Error] 'a_param' was not declared in this scope

or或者

[Error] call to 'Triangle::Triangle(int, int, double)' uses the default argument for parameter 3, which is not yet defined

So, please, can someone who can fix this answer the question?所以,请问,可以解决这个问题的人可以回答这个问题吗?

Thanks.谢谢。

As the compiler is pointing out, the other constructor arguments are not available as default parameters for the c_param argument.正如编译器所指出的,其他构造函数参数不可用作 c_param 参数的默认参数。 Rather than using default values, just overload the constructor, including one that just accepts 2 parameters.与其使用默认值,不如重载构造函数,包括只接受 2 个参数的构造函数。 This constructor can then invoke the other constructor that accepts all 3:这个构造函数然后可以调用另一个接受所有 3 的构造函数:

 // Constructor overload that accepts all 3 parameters
 Triangle(int a_param, int b_param, double c_param):
  a(a_param), b(b_param), c(c_param) {
  }

  // Constructor overload that accepts just a and b, call the other constructor
  // to set all 3 members
  Triangle(int a_param, int b_param):
  Triangle(a_param, b_param, Phythagorean_Hypotenuse(a_param, b_param)) {
  }

Default parameter values cannot reference other parameters.默认参数值不能引用其他参数。 You can define two overloads, one of which delegates to the other, to do what you want:您可以定义两个重载,其中一个委托给另一个,以执行您想要的操作:

class Triangle {
public:
    double a;
    double b;
    double c;

    Triangle(double a_param, double b_param, double c_param)
        : a{a_param},
          b{b_param},
          c{c_param}
    {}

    Triangle(double a_param, double b_param)
        : Triangle{a_param, b_param, Phythagorean_Hypotenuse(a_param, b_param)}
    {}
};

Live Demo现场演示


A few other notes:其他一些注意事项:

  • Class constructors do not have a return type.类构造函数没有返回类型。 I changed void Triangle(...) to Triangle(...)我将void Triangle(...)更改为Triangle(...)
  • I used constructor initialization lists instead of assignment in the constructor's body.我在构造函数的主体中使用了构造函数初始化列表而不是赋值。 There's likely no difference for small primitive values like int s or double s, but it's a good habit to get into and can make a big difference for more complex types对于像int s 或double s 这样的小原始值可能没有区别,但这是一个好习惯,并且可以对更复杂的类型产生很大的影响
  • int doesn't make sense for the type of c (or a or b for that matter). intc的类型(或ab就此而言)没有意义。 The sides of a triangle are unlikely to all be integers三角形的边不可能都是整数
  • There's no reason to pass parameters to Pythagorean_Hypotenuse by reference.没有理由通过引用将参数传递给Pythagorean_Hypotenuse It's simpler and likely faster to pass them by value按值传递它们更简单,也可能更快

There are some issues that prevent your code from compiling, namely:有一些问题会阻止您的代码编译,即:

  1. Constructors do not have return type.构造函数没有返回类型。
  2. double c_param = Phythagorean_Hypotenuse(a_param, b_param) is not valid for a parameter, a_param, b_param will not be recognized. double c_param = Phythagorean_Hypotenuse(a_param, b_param)对参数a_param, b_param将无法识别a_param, b_param

Recommend change:建议更改:

Since the result of a hypothenuse calculation will most likely be a decimal value, c should be a double .由于假设计算的结果很可能是十进制值,因此c应该是double

You can do something like this:你可以这样做:

Running sample运行示例

#include <iostream>
#include <cmath>

double Phythagorean_Hypotenuse (int& a, int& b ) {
    return sqrt((a * a) + (b * b));
};

class Triangle {
    public:
      int a;
      int b;
      double c; //should be double

      //initializer list is a good practice for member initialization
      Triangle(int a_param, int b_param) 
          : a(a_param), b(b_param), c(Phythagorean_Hypotenuse(a, b)) {} 
};

int main(){

    Triangle mytri_1(10, 20);
    std::cout << mytri_1.a << std::endl;
    std::cout << mytri_1.b << std::endl;
    std::cout << mytri_1.c << std::endl;
}

Output:输出:

10
20
22.3607

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

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