gpt4 book ai didi

c++ - 我可以从另一个构造函数定义一个构造函数吗?

转载 作者:行者123 更新时间:2023-12-05 08:15:11 25 4
gpt4 key购买 nike

<分区>

这里有一个虚拟的例子来说明:

class C
{
// complex class with many fields and methods
// including very expensive:
int computeA();
int computeB();
}

struct S
{
S(int a, int b); // initializes as {a, b, a*b};
// how to define below constructor?
S(const C& c); // Should be equivalent to calling S(c.computeA(), c.computeB())

int a;
int b;
int ab;
}

我可能遗漏了一些简单的东西,但我所有的尝试在语法上都不正确。我显然可以通过使用辅助函数而不是构造函数来规避这个问题,但是是否有直接定义此类构造函数的正确方法?

当然我不想这样做:

S(const C& c); // initialize as {c.computeA(), c.computeB(), c.computeA()*c.computeB()};

因为重复不必要的昂贵计算。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com