简体   繁体   中英

Constructors and inheritance c++

this a constructor of class TXTArgon, which is a subclass of Particule. "m_" is an attribute of the class Particule. I need to specify the m_ of TXTArgon par default with value 18.0. I get an error : " class 'TXTArgon' doesn't have any field named 'm_' ". But m_ is in protected...

TXTArgon::TXTArgon () 
: Particule(),
  m_(18.0),
  nom_("TXTAr")
{
}

Does anybody see how can I correct this code?

You can only initialize m_ via a constructor of Particule . If Particule doesn't have a suitable constructor, then what you can do is change its value after it has been initialized:

TXTArgon::TXTArgon () 
: Particule(),
  nom_("TXTAr")
{
  m_ = 18.0; // or m_ = TheTypeOfM_(18.0)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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