简体   繁体   English

在类中初始化可选的常量引用

[英]Initialization of an optional constant reference in a class

I have two classes, A and B. Class A is a transformation (a matrix) that performs a transformation on a given vector. 我有两个类,A和B。类A是对给定向量执行转换的转换(矩阵)。

class A{ 
public:
     ...
     A(...){};
     ...
     void func_A(std::vector<double>& vec){
        /* Transform vector vec */
     }
};

Class B has two members; B类有两名成员; std::vector<double> &vec a reference of a vector, and const std::vector<std::shared_ptr<A> > &a_ptrs a constant reference of another vector containing shared pointers of class A, representing different transformations. std::vector<double> &vec是向量的引用,而const std::vector<std::shared_ptr<A> > &a_ptrs是另一个向量的常量引用,该向量包含类A的共享指针,表示不同的转换。 a_ptrs may contain zero, one or several transformations. a_ptrs可以包含零个,一个或几个转换。 One of the jobs of class B is to apply these (if any) transformations on vector vec . B类的工作之一是将这些(如果有的话)转换应用于向量vec

class B{
public:
    std::vector<double> &vec;
    const std::vector<std::shared_ptr<A> > &a_ptrs;

    B(std::vector<double> &vec_ref) : vec(vec_ref){
     /* How to initialize a_ptrs if there are no transformations available? 
      That is, there are no shared pointers of class A available.*/
    }        

    B(std::vector<double> &vec_ref,
      const std::shared_ptr<A> &aptr) : vec(vec_ref){
      /* How to initialize a_ptrs if there is only one transformation available, and 
      I just decide to pass a const reference to the shared pointer of A? */
    } 

    // No issues with this constructor:
    B(std::vector<double> & vec_ref, 
      const std::vector<std::shared_ptr<A> > &aptr) : vec(vec_ref), a_ptrs(aptr){}

    void func_B(){
         ...
         // Apply the transforms:
         for(int i=0; i<a_ptrs.size(); ++i){
             a_ptrs[i]->func_A(vec);
         }
         ....
    }
};

For this purpose, as you can see, I have overloaded the constructor of class B. When const std::vector<std::shared_ptr<A> > &a_ptrs is passed as argument to the constructor of B, everything is fine. 为此,您可以看到,我已经重载了类B的构造函数。当将const std::vector<std::shared_ptr<A> > &a_ptrs作为参数传递给B的构造函数时,一切都很好。 But my problem is that I simply don't know how to initialize this constant reference for the cases where there are zero or only one transformation available, ie, a_ptrs is empty or has only one element, respectively. 但是我的问题是,对于只有零个或只有一个转换可用的情况,即a_ptrs为空或只有一个元素的情况,我根本不知道如何初始化此常量引用。

If a_ptrs has only one element, I want to be able to just pass a const std::shared_ptr<A> &aptr , and initialize a_ptrs somehow based on that. 如果a_ptrs只有一个元素,我希望能够只传递一个const std::shared_ptr<A> &aptr ,并a_ptrs基础初始化a_ptrs

I also don't want to make any copies of the shared pointer to class A in class B. I want to have a constant reference to the shared pointer as well. 我也不想在类B中为类A创建共享指针的任何副本。我也希望对共享指针也有一个常量引用。

Based on what I've found on the internet, there is a possibility of using boost::optional or std::experimental::optional , but I couldn't make it work. 根据我在互联网上找到的信息,可以使用boost::optionalstd::experimental::optional ,但是我无法使其正常工作。

I'm fairly new to c++, and I've been working on this issue for two days without any luck. 我对C ++还是相当陌生,并且在此问题上进行了两天的工作,没有任何运气。 How can I overcome this problem? 我该如何克服这个问题? Should I have another design strategy? 我应该有其他设计策略吗? I would appreciate any comments or suggestions that will help me solve this problem. 我将不胜感激任何可以帮助我解决此问题的意见或建议。

References MUST be initialized, no exceptions. 引用必须被初始化,没有例外。

However, in your situation, you can get around this by having an empty vector at hand to handle these cases: 但是,根据您的情况,您可以通过使用空向量来处理以下情况来解决此问题:

class B {
  static const std::vector<std::shared_ptr<A> > empty_a;

  std::vector<double> &vec;
  const std::vector<std::shared_ptr<A> > &a_ptrs;
public:

  B(std::vector<double> &vec_ref) 
    : vec(vec_ref), 
      a_ptrs(empty_a) {}
};

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

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