我在这里问了这个问题:C++ Method chaining with classes
本质上,我想做的是使用方法链接从另一个类调用构造函数/方法。假设我有 2 个课程:
class Signal {
    public:
      Signal() { } // constructor
      Signal& ParseSignal() { 
          // In this method I want to call 
          // the constructor "Parse()" 
      }
    protected:
       std::vector<double> data;
};
我还有另一堂课叫Parse:
class Parse {
   public:
      Parse() {
        // This is the implementation
        // I need to access the "data" contained in class "Signal
};
我的主要目标是主要执行以下操作:
Signal s = Signal().ParseSignal();
然后这将接受信号,并解析这个。
然而,有人建议我应该使用CRTP,因为基类(在这种情况下Signal)必须有一个template<>参数,由于其他类继承,这是不可能的。
这个问题还有其他解决方案吗?
编辑:
我尝试了以下方法,但是,它看起来像一个dirty实现,我无法访问成员变量:
class Parser {
public:
    Parser() { 
        parse();    
    }
    void parse() {
        cout << "YES";
    }
 };
 class Signal {
public:
    friend class Parser; 
    Signal() { val = 0;}
    Signal& Parse() {
        Parser::Parser();
        return *(this);
    }
protected:
    int val;
 };