V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
opiviqo
V2EX  ›  C++

请教大佬们一个 c++的模板的问题。

  •  
  •   opiviqo · 2022-06-29 10:17:47 +08:00 · 1714 次点击
    这是一个创建于 639 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1.定义一个类模板 A;
    template <typename typeA> class A{};


    2.定义一个类 B ,初始化需要参数 x,y;
    class B{
    B(int x,int y);
    };
    3.然后用 B 类去套模板:
    A<B> a;
    4.问题:B 套模板的时候,该如何初始化参数 x ,y 呢?
    12 条回复    2022-07-14 17:22:54 +08:00
    SJ2050cn
        1
    SJ2050cn  
       2022-06-29 10:26:00 +08:00   ❤️ 1
    你既然把 B 当成 A 的模板参数,那么 A 类里面应该有 B 类型的属性,可以在 A 类构造函数的初始化列表中对该属性进行初始化,或者你只是在 A 类中的方法中用到了 B 类型的形参,那 B 类型应该在调用 A 类方法前初始化的。
    newmlp
        2
    newmlp  
       2022-06-29 10:27:24 +08:00   ❤️ 1
    B 类怎么初始化,套模板后还是怎么初始化啊
    opiviqo
        3
    opiviqo  
    OP
       2022-06-29 10:28:58 +08:00
    @newmlp 如果按我你不给 b 参数 会编译不过;
    newmlp
        4
    newmlp  
       2022-06-29 10:41:11 +08:00
    @opiviqo 原来:B b(1,2)
    现在:A<B> a(1,2)
    ysc3839
        5
    ysc3839  
       2022-06-29 10:53:23 +08:00   ❤️ 1
    请发完整代码
    stein42
        6
    stein42  
       2022-06-29 11:13:42 +08:00
    利用模版构造函数和可变长模版参数,按 C++ 11 标准可编译。

    ```
    template<typename T>
    class A {
    public:
    T t;

    template<typename ...ARGS>
    A(ARGS... args) : t{args...} {}
    };

    class B {
    public:
    B(int x, int y) {}
    };

    A<B> a{1, 2};
    ```
    stein42
        7
    stein42  
       2022-06-29 11:30:50 +08:00   ❤️ 1
    如果 B 的构造函数有引用参数,还需要用到 std::forward 。
    opiviqo
        8
    opiviqo  
    OP
       2022-06-29 11:56:34 +08:00
    @ysc3839
    template <typename typeA> class A{
    void Operation(double value) {
    type_a.Algorithm(value);
    }

    private:
    typeA type_a;
    };

    class B{
    public:
    B(int x1,int y1):x(x1),y(y1){}
    int x;
    int y;
    void Algorithm(double value){
    ;
    }
    };

    int main(){
    A<B> a;
    }
    leimao
        9
    leimao  
       2022-06-30 08:21:29 +08:00   ❤️ 1
    ```
    template <typename typeA> class A{

    A(int x1,int y1) : type_a{x1, y1} {}

    void Operation(double value) {
    type_a.Algorithm(value);
    }

    private:
    typeA type_a;
    };
    ```
    leimao
        10
    leimao  
       2022-06-30 08:27:33 +08:00
    如果是更 general 的 use case ,你需要用到 perfect forwarding 。你可以看一下:
    https://leimao.github.io/blog/Universal-Reference-Perfect-Forwarding/
    FrankHB
        11
    FrankHB  
       2022-06-30 08:56:58 +08:00   ❤️ 1
    问题不成立。
    A<B>里的构造函数直接取决于 A 的定义,而不一定依赖 B 。你不给 A 的明确定义,这个问题就无解,上面的多数回答也就是在这个基础上瞎蒙(假定 A 的构造函数里会初始化 B )。
    有没有依赖 B 的构造函数都是个问题,就更别说有没有依赖签名为(int, int)的这个重载了。
    segmentlll
        12
    segmentlll  
       2022-07-14 17:22:54 +08:00
    B 实例化的时候才会调用构造函数,你想想自己什么时候实例化? A<B> 这里只是告诉编译器找 type 是 B 的模板实现。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5376 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:38 · PVG 16:38 · LAX 01:38 · JFK 04:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.