结构的赋值运算符

时间:2016-11-30 20:53:55

标签: structure copy-constructor assignment-operator

我正在尝试为结构元素分配一些默认值。 我的结构如下:

struct SourceProperties
        {
            SoundTypes                                  m_SoundTypes;
            ChannelGroupType                            m_channelGroupType;
            SoundTrigger                                m_SoundTrigger;
            QE::CVector                                 m_SourcePosition;                               
            QE::CVector                                 m_SourceVelocity;                   
            QE::CVector                                 m_Up;                               
            QE::CVector                                 m_Forward;                      
            QE::CMatrix                                 m_SourceOrientation;                
            float                                       m_SourceIntensity;
            QE::CVector                                 m_SoundFactors;                             
        };

CVectorCMatrix是我们自己的引擎中定义的类,SoundTypesChannelGroupTypeSoundTrigger属于“枚举”类型。 我需要在另一个类的结构中定义元素值的默认值。更清楚的是,用户使用此结构的实例在另一个类中设置此结构的元素的值。这是一个pSeudo代码:

定义结构的类(头文件):

class  CSound
    {
    public:

struct SourceProperties
        {
            SoundTypes                                  m_SoundTypes;
            ChannelGroupType                            m_channelGroupType;
            SoundTrigger                                m_SoundTrigger;
            QE::CVector                                 m_SourcePosition;                               
            QE::CVector                                 m_SourceVelocity;                   
            QE::CVector                                 m_Up;                               
            QE::CVector                                 m_Forward;                      
            QE::CMatrix                                 m_SourceOrientation;                
            float                                       m_SourceIntensity;
            QE::CVector                                 m_SoundFactors;                             
        };

private: 
      SourceProperties m_SourceProperties;
};

The class in which we assign the default values (header and cpp file):

class CSoundComponent :
{
 private:
  CSound::SourceProperties  m_SoundProperties;
}

The cpp file:

QE::CSoundComponent()
{
    m_SoundProperties.m_SoundTypes          = CSound::SoundTypes::ST_3D;
    m_SoundProperties.m_SoundTrigger       =CSound::SoundTrigger::STR_EVENT;
    m_SoundProperties.m_Forward             = CVector(1.0f,0.0f,0.0f);
    m_SoundProperties.m_Up                  = CVector(0.0f, 1.0f, 0.0f);
    m_SoundProperties.m_SourcePosition      = CVector(0.0f, 0.0f, 0.0f);
    m_SoundProperties.m_channelGroupType=CFMODChannelGroup::ChannelGroupType::CGP_DEFAULT;
    m_SoundProperties.m_SourceIntensity     = 10000.0f;
    m_SoundProperties.m_SoundFactors        = CVector(1.0f, 1.0f, 1.0f);

oid QE::CSoundComponent::SetSoundProperties(QE::CSound::SourceProperties pSourceProperties)
{
    m_SoundProperties.m_SoundTypes          = pSourceProperties.m_SoundTypes;
    m_SoundProperties.m_channelGroupType    = pSourceProperties.m_channelGroupType;
    m_SoundProperties.m_SoundTrigger        = pSourceProperties.m_SoundTrigger;
    m_SoundProperties.m_SourcePosition      = pSourceProperties.m_SourcePosition;
    m_SoundProperties.m_SourceIntensity     = pSourceProperties.m_SourceIntensity;
    m_SoundProperties.m_SoundFactors        = pSourceProperties.m_SoundFactors; 
}



}

The class which user is utilizing(cpp):

class userclass
{

CSound::SourceProperties lSoundProperties;
lSoundProperties.m_SoundTypes = CSound::SoundTypes::ST_3D;
QE::CSoundComponent* lSoundComponent;
lSoundComponent->setSoundProperties(lSoundProperties);
}

由于某种原因,用户正在分配的所有值都已正确设置,除了浮点值之外,还设置了所有默认值。 有谁知道我应该如何定义赋值运算符“=”?谢谢。

0 个答案:

没有答案