【问题标题】:c++ Constructor initialization with instance of structurec ++构造函数初始化与结构实例
【发布时间】:2015-07-13 22:20:17
【问题描述】:

不知道如何将 m_Performance 放入 get_Performance

struct Performance
{
    double High;
    double Average;
    double Low;
}Perf;

创建结构

class Strategy
{ public:
Performance m_Performance(){
    Perf.High = 10.1;
    Perf.Average =5.1;
    Perf.Low =1.1;
};
void get_Performance(){
    m_Performance();     ///This part does not work
}
};

创建类并插入方法,get_Performance。出了点问题。

int _tmain(int argc, _TCHAR* argv[])
{
    Strategy a;
    cout << a.get_Performance << endl;
    return 0;
}

想从结构中获取数据成员

【问题讨论】:

  • 我创建了一个名为 Performance 的结构体和一个名为 Strategy 的类,想在该类中创建一个名为 m_Performance 的结构体实例和一个名为 get_Performance 的方法来返回 m_Performance。
  • 一个完整的最小工作示例将帮助我们了解您的问题。
  • 另外,使用“编辑”让您的问题更清晰,而不是添加 cmets。

标签: c++ struct constructor instance


【解决方案1】:

如果我理解正确,你应该:

struct Performance
{
    double High;
    double Average;
    double Low;
};

class Strategy
{ 
private:
    Performance m_Performance;
public:
    Strategy() {
        m_Performance.High = 10.1;
        m_Performance.Average = 5.1;
        m_Performance.Low = 1.1;
    }
    Performance get_Performance() {
       return m_Performance;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Strategy a;
    Performance p = a.get_Performance();
    return 0;
}

【讨论】:

  • 非常感谢!最后,想办法做到这一点。
猜你喜欢
  • 1970-01-01
  • 2015-08-08
  • 2011-01-06
  • 1970-01-01
  • 2015-04-28
  • 2012-11-05
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多