gpt4 book ai didi

c++ - 作为此类对象的类变量

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

标题可能令人困惑,但我想知道是否可以创建这样的程序:

class family_tree
{
private:
string name, surname;
family_tree father(); //fragile point!

public:
family_tree();
family_tree(string n, string sur");
void print();
};

标准对此类声明有何规定?编程的好习惯有哪些呢?危险吗?

此外,我不能使用第二个构造函数:

family_tree father("A","B");

编译器:

字符串常量前的预期标识符

在字符串常量之前需要 ',' 或 '...'

最佳答案

class family_tree
{
private:
string name, surname;
family_tree father(); //fragile point!

public:
family_tree();
family_tree(string n, string sur); // note that I removed a " here.
void print();
};

这是完全正确的。你的脆弱点一点都不脆弱——你有一个返回 family_tree 的函数,在 family_tree 对象上调用它并不重要。该语言是否允许您将 const char* 字符串文字隐式转换为 std::string,我不记得了。

关于c++ - 作为此类对象的类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4009171/

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