本文介绍了C ++ 0x初始化列表示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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:));
}

它会支持这样的东西:



Example1:

  std :: vector< Ask *> ui = {
AskString(输入您的姓名:,3,25),
AskString(输入您的城市:,2,25),
问(输入您的年龄: )
};

还是必须有这样的文字?:



Example2:

  std :: vector< Ask *> ui = {
{输入您的姓名:,3,25},
{输入您的城市:,2,25},
{输入您的年龄:} b $ b};

如果是这样,AskString和Ask之间的区别如何处理?






























  std :: vector< Ask *> ui = {
new AskString {输入您的姓名:,3,25},
new AskString {输入您的城市:,2,25},
new Ask {Enter您的年龄:}
};

这是允许的,没有类型歧义。



这也是正确的:

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

而你的例子更像:

  std :: vector< Ask> ui = {// not pointers 
{输入您的姓名:},
{输入您的城市:},
{输入您的年龄:}
} ;

std :: vector< AskString> uiString = {// not pointers
{输入您的姓名:,3,25},
{输入您的城市:,2,25},
{输入您的年龄:,7,42}​​
};

,并且类型也不会含糊。


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?

解决方案

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.

这篇关于C ++ 0x初始化列表示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:07