Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 初始化嵌套模板类_C++_Templates_C++11_Constructor - Fatal编程技术网

C++ 初始化嵌套模板类

C++ 初始化嵌套模板类,c++,templates,c++11,constructor,C++,Templates,C++11,Constructor,我有许多模板类,它们以任意顺序一起工作(共享相同的概念) 假设我有: template<typename T> class A { T t_; public: void call() { // Do something. t_.call(); } }; template<typename T, typename U> class B { T t_; U u_; public: void call() { // Do som

我有许多模板类,它们以任意顺序一起工作(共享相同的概念)

假设我有:

template<typename T>
class A {
  T t_;
public:
  void call() {
    // Do something.
    t_.call();
  }
};

template<typename T, typename U>
class B {
  T t_;
  U u_;
public:
  void call() {
    // Do something.
    t_.call();
    // Do something.
    u_.call();
  }
};

class C {
public:
  void call() {
    // Do something.
  }
};
我如何初始化Foo?或者任何其他嵌套类组合

我目前的解决方法是将C定义为:

template<typename I>
struct C {
  C() : var_(I::get())
  void call();
};
模板
结构C{
C():var(I::get())
无效调用();
};
并在函数内创建Foo:

int main()
{
  int i = 0;

  struct HelperC1 {
    static int get(bool set = false, int value = 0) {
      static int value_ = value;
      if (set) value_ = value;
      return value_;
    }
  } helperC1;

  struct HelperC2 {
    static int get(bool set = false, int value = 0) {
      static int value_ = value;
      if (set) value_ = value;
      return value_;
    }
  } helperC2;

  helperC1.get(true, i);
  helperC2.get(true, i+1);

  A<B<A<C<HelperC1>>,C<HelperC2>>> foo;   
  foo.call();

  return 0;
}
intmain()
{
int i=0;
结构帮助程序1{
静态int-get(布尔集=false,int值=0){
静态int值=值;
如果(设置)值=值;
返回值;
}
}助手1;
结构帮助器2{
静态int-get(布尔集=false,int值=0){
静态int值=值;
如果(设置)值=值;
返回值;
}
}助手2;
得到(真的,我);
helperC2.get(true,i+1);

A您可以使用指针并使用已构造和初始化的对象构建
foo
。即:

未经测试的代码

template<typename T>
 class A {
  T* t_;

public:
  A(T* valptr) : t_(valptr){}
  ~A(){ delete t_ ; }

  void call() {
    // Do something.
    t_.call();
  }
};

template<typename T, typename U>
class B {
  T* t_;
  U* u_;

public:
  B(T* val1ptr, U* val2ptr):t_(val1ptr), u_(val2ptr){}
  ~B(){delete val1ptr; delete val2ptr;}

  void call() {
    // Do something.
    t_->call();
    // Do something.
    u_->call();
  }
};

class C {
  private:
    int x_;
  public:
  C(int x):x_(x){}
  void call() {
    // Do something.
  }
};
模板
甲级{
T*T_;
公众:
A(T*valptr):T_3;(valptr){}
~A()
无效调用(){
//做点什么。
调用();
}
};
模板
B类{
T*T_;
U*U_;
公众:
B(T*val1ptr,U*val2ptr):T(val1ptr),U(val2ptr){}
~B(){delete val1ptr;delete val2ptr;}
无效调用(){
//做点什么。
t_->call();
//做点什么。
u_uu->call();
}
};
C类{
私人:
int x_2;;
公众:
C(int x):x_ux{}
无效调用(){
//做点什么。
}
};
用法:

A<B<A<C>,C>> foo( new B<A<C>,C>(new A<C>(new C(3) ), new C(3) ) );
A foo(新B(新A(新C(3)),新C(3));

此示例使用移动构造和转发帮助函数为分支模板类型构建值

转发帮助程序包装构造函数,使用类型推断来避免使用复杂参数指定每个构造函数

在本例中,call()函数转储对象子对象的参数结构和值

#include <iostream>
#include <utility>

template<typename T>
class A {
    T t;
public:
    A(T&& t) : t{std::move(t)} {}
    void call() {
        std::cout << "A<";
        t.call();
        std::cout << ">";
    }
};
template <typename T>
inline A<T> mkA(T&& t) { return A<T>{std::forward<T>(t)}; }

template<typename T, typename U>
class B {
    T t;
    U u;
public:
    B(T&& t, U&& u) : t{std::move(t)}, u{std::move(u)} {}

    void call() {
        std::cout << "B<";
        t.call();
        std::cout << ",";
        u.call();
        std::cout << ">";
    }
};
template <typename T, typename U>
inline B<T,U> mkB(T&& t, U&& u) { 
    return B<T,U>{std::forward<T>(t), std::forward<U>(u)};
}

class C {
    int c;
public:
    C(int c) : c{c} {}
    void call() {
        std::cout << "C(" << c << ")";
    }
};

int main() {
    auto bar = mkA(mkB(mkA(C{1}), mkB(mkB(mkA(C{2}),C{3}), C{4})));
    bar.call();
    std::cout << '\n';
}
#包括
#包括
模板
甲级{
T;
公众:
A(T&&T):T{std::move(T)}{
无效调用(){

std::cout By“maybuly”是指“正确”吗?为什么不为类
A
B
(以及潜在的getter)提供构造函数呢为什么要使用指针?没有指针你也可以获得相同的结果……而且你的类不再是RAII了……如果你创建了一个超出范围的副本,成员指针将被删除,类将被破坏。最好改用智能指针。@davidhigh你能演示如何在没有指针(或引用)的情况下获得相同的行为吗。虚拟公共父级将允许删除模板而不是指针。对于RAII,应实现适当的复制构造函数和赋值运算符。隐式共享成员类可能会导致其他错误,具体取决于用例。
template<typename T>
 class A {
  T* t_;

public:
  A(T* valptr) : t_(valptr){}
  ~A(){ delete t_ ; }

  void call() {
    // Do something.
    t_.call();
  }
};

template<typename T, typename U>
class B {
  T* t_;
  U* u_;

public:
  B(T* val1ptr, U* val2ptr):t_(val1ptr), u_(val2ptr){}
  ~B(){delete val1ptr; delete val2ptr;}

  void call() {
    // Do something.
    t_->call();
    // Do something.
    u_->call();
  }
};

class C {
  private:
    int x_;
  public:
  C(int x):x_(x){}
  void call() {
    // Do something.
  }
};
A<B<A<C>,C>> foo( new B<A<C>,C>(new A<C>(new C(3) ), new C(3) ) );
#include <iostream>
#include <utility>

template<typename T>
class A {
    T t;
public:
    A(T&& t) : t{std::move(t)} {}
    void call() {
        std::cout << "A<";
        t.call();
        std::cout << ">";
    }
};
template <typename T>
inline A<T> mkA(T&& t) { return A<T>{std::forward<T>(t)}; }

template<typename T, typename U>
class B {
    T t;
    U u;
public:
    B(T&& t, U&& u) : t{std::move(t)}, u{std::move(u)} {}

    void call() {
        std::cout << "B<";
        t.call();
        std::cout << ",";
        u.call();
        std::cout << ">";
    }
};
template <typename T, typename U>
inline B<T,U> mkB(T&& t, U&& u) { 
    return B<T,U>{std::forward<T>(t), std::forward<U>(u)};
}

class C {
    int c;
public:
    C(int c) : c{c} {}
    void call() {
        std::cout << "C(" << c << ")";
    }
};

int main() {
    auto bar = mkA(mkB(mkA(C{1}), mkB(mkB(mkA(C{2}),C{3}), C{4})));
    bar.call();
    std::cout << '\n';
}
A<B<A<C(1)>,B<B<A<C(2)>,C(3)>,C(4)>>>