简体   繁体   English

当模板类的输入输出类型不同时如何处理构造函数

[英]How to deal with constructor when input-output type is different for templated class

I'm learning C++ for only a short period of time and the following problem gives me headache.我只学习了很短的时间 C++,下面的问题让我很头疼。

What I'm trying to do is basiclly to wrap an existing library without introducing too much overhead such that the wrapper library could run as fast as the existing one.我想要做的基本上是在不引入太多开销的情况下包装现有库,以便包装库可以像现有库一样快速运行。 Therefore, I tend to not modify anything in existing library.因此,我倾向于不修改现有库中的任何内容。 I'm doing this in order to make the interface(syntax) compatible with my old codes.我这样做是为了使接口(语法)与我的旧代码兼容。

Say, the existing class is called BASE, which is also templated class.比如说,现有的类叫做 BASE,它也是模板化的类。 There are a few ways to do the wrapping such as inheritance.有几种方法可以进行包装,例如继承。 I decided to go with the option of including BASE as a member of Wrapper class for better encapsulation.我决定选择将 BASE 作为 Wrapper 类的成员,以便更好地封装。

        template<class T>    
        class Wrapper{
        public:
                     Wrapper() : base(){};
     /*error line*/  Wrapper( const Wrapper<double>& a, const Wrapper<double>& b ) : base(a.base, b.base){};
                    // constuct Wrapper<complex<double>> vector from two Wrapper<double> vectors using constuctor from existing class BASE
                    // 'a', 'b' are real and imag part respectively.

             private:
                     BASE<T>   base;
            }; 

The following line couldn't compile, error message is 'base is declared as private within the context'以下行无法编译,错误消息是“base 在上下文中被声明为私有”

      /*error line*/  Wrapper( const Wrapper<double>& a, const Wrapper<double>& b ) : base(a.base, b.base){};

Experiments I have done so far:到目前为止我做过的实验:

<1>. <1>。 change改变

      private:
      BASE<T>   base;

to

      public:
      BASE<T>   base;

the code complies and gives correct answer.代码符合并给出正确答案。 But, as I stated above, I want data encapsulation, so this solution is a no go.但是,正如我上面所说,我想要数据封装,所以这个解决方案是不行的。

<2>. <2>。 Although the error message suggests there is something to do with the access privilege, I think this is caused by different input-output type (inputs are two "double", output is type of "complex double").尽管错误消息表明与访问权限有关,但我认为这是由不同的输入输出类型引起的(输入是两个“双”,输出是“复双”类型)。 The following dosomething() function works as long as the type of input and type of (*this) are consistent, no error regarding 'base is declared as private within the context'.只要输入类型和 (*this) 类型一致,下面的 dosomething() 函数就可以工作,没有关于“base 在上下文中被声明为私有”的错误。

    template<class T>    
    class Wrapper{
    public:
                 Wrapper() : base(){};
 /*error line*/  Wrapper( const Wrapper<double>& a, const Wrapper<double>& b ) : base(a.base, b.base){};

     void dosomething(const Wrapper<T>& a)
     {
      (*this).base = a.base; // ok, compiles good
     }

         private:
                 BASE<T>   base;
        }; 

How to work around?如何解决? Look forwarding to any useful comments.期待任何有用的评论。

You may make your class friend of the same class but with different parameter:你可以让你的班级成为同一班级的friend ,但使用不同的参数:

template<class T>    
class Wrapper{
    template <typename U> friend class Wrapper;
public:
    Wrapper() : base(){}
    Wrapper(const Wrapper<double>& a, const Wrapper<double>& b) : base(a.base, b.base){}

private:
    BASE<T> base;
}; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM