微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 initializer_list 和模板实现具有成员变量 vector <Class B> 的 Class A 的构造函数,这是它的 Base 函数?

如何解决如何使用 initializer_list 和模板实现具有成员变量 vector <Class B> 的 Class A 的构造函数,这是它的 Base 函数?

所以问题是:

  1. 类 Stan 必须有一个构造函数,使其能够接收和存储无限数量的元素,并且必须使用模板来实现。另外,在main函数中演示一下
  2. 类 Stan 必须有一个构造函数,使其能够接收和存储无限数量的元素,并且必须使用初始化列表来实现。另外,在主函数中演示
  3. 将类 Stan 放入命名空间 Zgrada
  4. 让我们假设有两种类型的斯坦 - Apa 和 Gar。在 main 函数中,有必要通过实现 getVrsta() 来演示多态性;返回两种类型之一

我不知道如何做更多的事情。 感谢您的帮助!

#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <iterator>
using namespace std;

int Element::brV = 0;



    class Element {
    public:
        string naziv;
        double obujam;
        static int brV;
        Element(string n,double o) : naziv(n),obujam(o) {
    
            if (o > 3.) brV++;
        }

        int getVelikiElementi(){
            return brV;
        }

        void virtual GetVrsta(){
            cout << "Vrsta Stana: ";
        }
    };



    template <class T>               //I think i got the templates_init right
    class Stan : public Element { 
    public:
        vector<Element> Elementi;

        template<class...T>
        Stan(T...arg) : Elementi({arg...}){} 


        Stan(initializer_list<Elementi>) : Element (){} // But this most certainly not
        
        void GetVrsta(){
            
        }
    };
    

int main () {

    Element X("a",3.);
    Element Y("b",2.);
    Element Z("c",1.);

    vector <Element*> E={X,Y,Z}; // initilizer_lista_const_call
    
    return 0;
}```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。