gpt4 book ai didi

c++ - 类成员函数返回的任意值

转载 作者:行者123 更新时间:2023-11-28 01:24:34 25 4
gpt4 key购买 nike

我正在处理一个由许多 .cxx 文件组成的项目。现在我想编辑一个类,添加一些变量和函数(这是下面注释的行)

#ifndef TN_LORENTZCHARGEVECTOR_
#define TN_LORENTZCHARGEVECTOR_
#include<iostream>
#include <TLorentzVector.h>

#include "common/Helpers.h"

class LorentzChargeVector : public TLorentzVector
{
public:
inline LorentzChargeVector()
: TLorentzVector(),
_charge(0) {}

inline LorentzChargeVector(const TLorentzVector &vec,
float charge)
: TLorentzVector(vec),
_charge(charge) {}

inline LorentzChargeVector(const LorentzChargeVector &v)
: TLorentzVector(v),
_charge(v.Charge()) {}

inline float Charge() const { return _charge; }
inline void SetCharge(float charge) { _charge = charge; }
/*
inline float Trkd0() {std::cout << "fnction" << _trkd0 << std::endl;return _trkd0;}
inline void SetTrkd0(float trkd0) {std::cout << '\t' << trkd0 << std::endl; _trkd0 = trkd0;std::cout << '\t' << _trkd0 << std::endl;}

inline float Trkd0sig() {return _trkd0sig;}
inline void SetTrkd0sig(float trkd0sig) {_trkd0sig = trkd0sig;}

inline float Trkz0() {return _trkz0;}
inline void SetTrkz0(float trkz0) {_trkz0 = trkz0;}

inline float Trkz0sintheta() {return _trkz0sintheta;}
inline void SetTrkz0sintheta(float trkz0sintheta) {_trkz0sintheta = trkz0sintheta;}
*/
inline LorentzChargeVector &operator=(const LorentzChargeVector &q)
{
TLorentzVector::operator=(q);
_charge = q.Charge();
return *this;
}

inline LorentzChargeVector operator+(const LorentzChargeVector &q) const
{
return LorentzChargeVector(TLorentzVector::operator+(q), _charge + q.Charge());
}

inline LorentzChargeVector &operator+=(const LorentzChargeVector &q)
{
TLorentzVector::operator+=(q);
_charge += q.Charge();
return *this;
}

inline LorentzChargeVector operator-(const LorentzChargeVector &q) const
{
return LorentzChargeVector(TLorentzVector::operator-(q), _charge - q.Charge());
}

inline LorentzChargeVector &operator-=(const LorentzChargeVector &q)
{
TLorentzVector::operator-=(q);
_charge -= q.Charge();
return *this;
}

inline LorentzChargeVector operator-() const
{
return LorentzChargeVector(TLorentzVector::operator-(), -_charge);
}

inline Bool_t operator==(const LorentzChargeVector &q) const
{
return (Vect() == q.Vect() && T() == q.T() && Charge() == q.Charge());
}

inline Bool_t operator!=(const LorentzChargeVector &q) const
{
return (Vect() != q.Vect() || T() != q.T() || Charge() != q.Charge());
}

// Print
inline void Print(Option_t *option="") const override
{
UNUSED(option)

Printf("(x,y,z,t)=(%f,%f,%f,%f) (P,eta,phi,E)=(%f,%f,%f,%f) charge=%f",
X(), Y(), Z(), E(),
P(), Eta(), Phi(), E(), _charge);
}


private:
float _charge;
// float _trkd0;
// float _trkd0sig;
// float _trkz0;
// float _trkz0sintheta;

ClassDefOverride(LorentzChargeVector, 1);
};

#endif // TN_LORENTZCHARGEVECTOR_

我这里的问题是由返回值的函数给出的。设置值的函数 (SetTrkd0 ecc.) 工作正常。相反,返回值的函数(Trkd0Trkd0sig ecc.)给我一个任意值。

我认为错误是由于构造函数引起的,它们没有被编辑为新变量。

最佳答案

您的构造函数需要显式初始化所有成员,否则它将存储您认为是垃圾(任意值)的不确定值。在您的情况下,_trkd0_trkd0sig_trkz0_trkz0sintheta 未初始化。

此外,请考虑将类的实现和声明分开。它可能允许您独立于接口(interface)更改实现,并且在处理像您的情况这样的更改需求时,这可能会更容易。

关于c++ - 类成员函数返回的任意值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54522977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com