不同类对象的数组

时间:2017-06-12 11:24:08

标签: c++ arrays object

在Baza类中,我需要创建一个数组,该数组将包含ZadanieTekst,ZadanieNumer,Zadanie4Odp类中的对象。我怎样才能做到这一点?

public async Task<ActionResult> Index(string language)
{
    if (String.IsNullOrWhiteSpace(language) == false)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
    }

    else if (String.IsNullOrWhiteSpace(language))
    {
        string userIpAddress = this.Request.UserHostAddress;

        var client = new HttpClient
        {
            BaseAddress = new Uri("http://freegeoip.net/xml/")
        };

        var response = await client.GetAsync(userIpAddress);

        var content = await response.Content.ReadAsStringAsync();

        var result = (Response)new XmlSerializer(typeof(Response)).Deserialize(new StringReader(content));

        // do stuff
    }

    ...
}

[XmlRoot(ElementName = "Response")]
public class Response
{
    [XmlElement(ElementName = "IP")]
    public string IP { get; set; }
    [XmlElement(ElementName = "CountryCode")]
    public string CountryCode { get; set; }
    [XmlElement(ElementName = "CountryName")]
    public string CountryName { get; set; }
    [XmlElement(ElementName = "RegionCode")]
    public string RegionCode { get; set; }
    [XmlElement(ElementName = "RegionName")]
    public string RegionName { get; set; }
    [XmlElement(ElementName = "City")]
    public string City { get; set; }
    [XmlElement(ElementName = "ZipCode")]
    public string ZipCode { get; set; }
    [XmlElement(ElementName = "TimeZone")]
    public string TimeZone { get; set; }
    [XmlElement(ElementName = "Latitude")]
    public string Latitude { get; set; }
    [XmlElement(ElementName = "Longitude")]
    public string Longitude { get; set; }
    [XmlElement(ElementName = "MetroCode")]
    public string MetroCode { get; set; }
}

我正在为我的学校项目做这件事,而我无法解决的问题。 在此先感谢您的帮助:)

1 个答案:

答案 0 :(得分:-2)

我认为,如果不使用std :: unique_ptr或std :: array做太多工作,这样的事情会很有用:

#include <vector>

...

class Baza
{
    public:
        std::vector<Zadanie*> my_vector;

};

...

int main(void)
{
    Baza my_baza;

    // Remember to properly destroy objects if you plan to
    // use this approach  without memory leaks!
    my_baza.my_vector.push_back(new ZadanieTekst(//Insert Params//));
    my_baza.my_vector.push_back(new ZadanieNumer(//Insert Params//));
    my_baza.my_vector.push_back(new Zadanie4Odp(//Insert Params//));

    ...

    return 0;
}