C++0x 初始值设定项列表示例

发布于 2024-07-22 03:48:22 字数 1048 浏览 4 评论 0原文

我想看看这个现有代码示例如何利用 C++0x 初始化列表功能。

示例 0:

#include <vector>
#include <string>
struct Ask {
    std::string prompt;
    Ask(std::string a_prompt):prompt(a_prompt){}
};
struct AskString : public Ask{
    int min;
    int max;
    AskString(std::string a_prompt, int a_min, int a_max):
        Ask(a_prompt), min(a_min), max(a_max){}
};
int main()
{
    std::vector<Ask*> ui;
    ui.push_back(new AskString("Enter your name: ", 3, 25));
    ui.push_back(new AskString("Enter your city: ", 2, 25));
    ui.push_back(new Ask("Enter your age: "));
}

它是否支持这样的内容:

示例 1:

std::vector<Ask*> ui ={
    AskString("Enter your name: ", 3, 25),
    AskString("Enter your city: ", 2, 25),
    Ask("Enter your age: ")
    };

或者它必须具有这样的文字吗?:

示例 2:

std::vector<Ask*> ui ={
    {"Enter your name: ", 3, 25},
    {"Enter your city: ", 2, 25},
    {"Enter your age: "}
    };

如果是这样,将如何处理 AskString 和 Ask 之间的差异?

I would like to see how this example of existing code would be able to take advantage of the C++0x initializer list feature.

Example0:

#include <vector>
#include <string>
struct Ask {
    std::string prompt;
    Ask(std::string a_prompt):prompt(a_prompt){}
};
struct AskString : public Ask{
    int min;
    int max;
    AskString(std::string a_prompt, int a_min, int a_max):
        Ask(a_prompt), min(a_min), max(a_max){}
};
int main()
{
    std::vector<Ask*> ui;
    ui.push_back(new AskString("Enter your name: ", 3, 25));
    ui.push_back(new AskString("Enter your city: ", 2, 25));
    ui.push_back(new Ask("Enter your age: "));
}

Would it support something like this:

Example1:

std::vector<Ask*> ui ={
    AskString("Enter your name: ", 3, 25),
    AskString("Enter your city: ", 2, 25),
    Ask("Enter your age: ")
    };

Or must it have literals like this?:

Example2:

std::vector<Ask*> ui ={
    {"Enter your name: ", 3, 25},
    {"Enter your city: ", 2, 25},
    {"Enter your age: "}
    };

If so how would the difference between AskString and Ask be handled?

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

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

发布评论

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

评论(2

想你的星星会说话 2024-07-29 03:48:22

当您请求指针时,您最后的示例将不被允许,而是尝试提供本地临时对象。

std::vector<Ask*> ui ={
    new AskString{"Enter your name: ", 3, 25},
    new AskString{"Enter your city: ", 2, 25},
    new Ask{"Enter your age: "}
    };

这是允许的,并且不会有类型歧义。

这也是对的:

std::vector<Ask*> ui ={
        new AskString("Enter your name: ", 3, 25),
        new AskString("Enter your city: ", 2, 25),
        new Ask("Enter your age: ")
        };

你的例子更像是:

std::vector<Ask> ui ={  // not pointers
    {"Enter your name: "},
    {"Enter your city: "},
    {"Enter your age: "}
    };

std::vector<AskString> uiString ={  // not pointers
    {"Enter your name: ", 3, 25},
    {"Enter your city: ", 2, 25},
    {"Enter your age: ", 7, 42}
    };

类型上不会有任何歧义。

You last examples wouldn't be allowed as you ask for pointers but try to provide local temporary objects instead.

std::vector<Ask*> ui ={
    new AskString{"Enter your name: ", 3, 25},
    new AskString{"Enter your city: ", 2, 25},
    new Ask{"Enter your age: "}
    };

That would be allowed and there would be no type ambiguity.

That would be right too :

std::vector<Ask*> ui ={
        new AskString("Enter your name: ", 3, 25),
        new AskString("Enter your city: ", 2, 25),
        new Ask("Enter your age: ")
        };

And your example is more like :

std::vector<Ask> ui ={  // not pointers
    {"Enter your name: "},
    {"Enter your city: "},
    {"Enter your age: "}
    };

std::vector<AskString> uiString ={  // not pointers
    {"Enter your name: ", 3, 25},
    {"Enter your city: ", 2, 25},
    {"Enter your age: ", 7, 42}
    };

and again there would be no ambiguity on the types.

雨夜星沙 2024-07-29 03:48:22

C++ 初始值设定项列表是同质的,这意味着它必须具有相同的类型,因此示例#2 已不再适用。 如果您在示例 1 中使用了 new,那么它就可以工作。

A c++ initializer list is homogenous, meaning it must have all the same type, so example #2 is out. If you used new in example 1, it would work.

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