具有默认值和值的构造函数不同的构造函数

发布于 2024-12-05 00:19:09 字数 474 浏览 0 评论 0原文

我想做这样的事情

class foo{
private:
    double a,b;

public:
    foo(double a=1, double b=2){
        this.a=a;
        this.b=b;
    }
}

int main(){
    foo first(a=1);
    foo first(b=2);
}

这样的事情可能吗还是我需要创建两个新的构造函数?

2.问题来了:这两个构造函数有什么区别:

class foo{
private:
    int a;

public:
    foo(int in):a(in){}
}

或者

class foo{
private:
    int a;

public:
    foo(int in){a=in}
}

I would like to do something like this

class foo{
private:
    double a,b;

public:
    foo(double a=1, double b=2){
        this.a=a;
        this.b=b;
    }
}

int main(){
    foo first(a=1);
    foo first(b=2);
}

Is such thing possible or do I need to create two new constructors?

Here comes the 2. question: What is the difference in those two constructors:

class foo{
private:
    int a;

public:
    foo(int in):a(in){}
}

or

class foo{
private:
    int a;

public:
    foo(int in){a=in}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

非要怀念 2024-12-12 00:19:10
foo first(a=1);
foo first(b=2);

在 C++ 中你不可能真正拥有这个。它曾经被考虑标准化,但后来被放弃。 Boost.Parameter 尽可能地近似命名参数,请参阅
http://www.boost.org/doc /libs/1_47_0/libs/parameter/doc/html/index.html

foo(int in):a(in){}
foo(int in){a=in}

第一个构造函数正在初始化 a,而第二个构造函数正在为其赋值。对于这个特殊情况(int)来说没有太大区别。

foo first(a=1);
foo first(b=2);

You cannot really have this in C++. It was once considered for standarization but then dropped. Boost.Parameter does as much as it can to approximate named parameters, see
http://www.boost.org/doc/libs/1_47_0/libs/parameter/doc/html/index.html

foo(int in):a(in){}
foo(int in){a=in}

The first constructor is initializating a, while the second is assigning to it. For this particular case (int) there isn't much of a difference.

不必在意 2024-12-12 00:19:10

在 C++ 中,this 指针是一个指针,而不是对象(在 java 中,您可以使用 . 访问它) >)。也就是说,您需要一个取消引用箭头(即 this->member(*this).member)。

无论如何,这是可以做到的。然而,C++ 中的参数化以相反的顺序工作,并且不能命名。例如,int test(int a=2, int b=42)int test(int a, int b=42) 都是合法的。但是,int test(int a=2, b) 是不合法的。

至于你的第二个问题,这个例子中的构造函数之间没有显着的区别。存在一些微小的(潜在的)速度差异,但在这种情况下它们很可能可以忽略不计。在第一个中,您使用初始化列表(继承和调用基类的构造函数(如果需要)所需),第二个只是显式设置变量的值(即使用operator=())。

In C++, the this pointer is a pointer not an object (in java, you would access it with the .). That said, you would need a dereferencing arrow (i.e. this->member or (*this).member).

In any case, this can be done. However, parameterization in C++ works in reverse order and cannot be named. For instance, int test(int a=2, int b=42) is legal as well as int test(int a, int b=42). However, int test(int a=2, b) is not legal.

As for your second question, there is no significant difference between the constructors in this example. There are some minor (potential) speed differences, but they are most-likely negligible in this case. In the first one you are using an initializer list (required for inheritance and calling the constructor of the base class if need be) and the second one is simply explicitly setting the value of your variable (i.e. using operator=()).

网名女生简单气质 2024-12-12 00:19:10

对于您的示例来说,这可能有点过分了,但您可能想了解 命名参数惯用语

It's probably overkill for your example, but you might like to learn about the Named Parameter Idiom.

月亮是我掰弯的 2024-12-12 00:19:10

C++ 不支持命名参数,因此:

int main()
{ 
  foo first(a=1); 
  foo first(b=2); 
} 

不合法。您还有多个非唯一标识符(例如first)。

C++ doesn't support named parameters so this:

int main()
{ 
  foo first(a=1); 
  foo first(b=2); 
} 

Is not legal. You also have multiple non-unique idenfiers (e.g. first).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文