How to initialize a static member object?

I did not know that I did not know this :). and a similar question didn’t help here .

So I ask. Please consider the following class:

//in Agent.h class Agent : public ns3::Object{ private: //... static BaseWifi m_wifi; //... }; 

:

 //Agent.cpp BaseWifi temp; BaseWifi Agent::m_wifi = temp; 

very different from this:

 //Agent.cpp BaseWifi Agent::m_wifi = BaseWifi(); 

The second approach does not work for me. why and how?

I don’t want to bother you with more codes, because I ran into this problem deep in my program. The program generates seg errors, since objects (members) inside the BaseWifi constructor BaseWifi not initialized correctly. when uninitialized internal elements are used, segregation occurs.

Thank you in advance for your comments and responses.

ps: Actually, I found this problem when I did not initialize this static member, and I deleted the extra line:

 BaseWifi temp; 

in my main() , which added more to my confusion !!! (this may depend on what I entered into the BaseWifi constructor, so don't mind it now)

Update-1: For those who would like to see BaseWifi:

 class BaseWifi { ns3::WifiHelper m_wifiHelper; // a wifi helper apply to setup vehicles Wifi ns3::NqosWifiMacHelper m_wifiMacHelper; // a wifi mac helper apply to setup vehicles Wifi ns3::YansWifiPhyHelper m_wifiPhyHelper; // a wifi phy helper apply to setup vehicles Wifi std::string m_phyMode; double m_rss; // -dBm bool m_init_done; public: BaseWifi(); virtual void init(); ns3::NetDeviceContainer Install(ns3::NodeContainer &c); virtual ~BaseWifi(); }; BaseWifi::BaseWifi() { m_init_done = false; m_rss = -80; m_phyMode ="DsssRate1Mbps"; // TODO Auto-generated constructor stub init(); } void BaseWifi::init() { NS_LOG_UNCOND("inside BaseWifi::init()"); m_wifiHelper.SetStandard (ns3::WIFI_PHY_STANDARD_80211b); m_wifiPhyHelper = ns3::YansWifiPhyHelper::Default (); // This is one parameter that matters when using FixedRssLossModel // set it to zero; otherwise, gain will be added m_wifiPhyHelper.Set ("RxGain", ns3::DoubleValue (0) ); // ns-3 supports RadioTap and Prism tracing extensions for 802.11b m_wifiPhyHelper.SetPcapDataLinkType (ns3::YansWifiPhyHelper::DLT_IEEE802_11_RADIO); ns3::YansWifiChannelHelper wifiChannel; wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); // The below FixedRssLossModel will cause the rss to be fixed regardless // of the distance between the two stations, and the transmit power wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",ns3::DoubleValue (m_rss)); m_wifiPhyHelper.SetChannel (wifiChannel.Create ()); // Add a non-QoS upper mac, and disable rate control m_wifiMacHelper = ns3::NqosWifiMacHelper::Default (); m_wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode",ns3::StringValue (m_phyMode), "ControlMode",ns3::StringValue (m_phyMode)); // Set it to adhoc mode m_wifiMacHelper.SetType ("ns3::AdhocWifiMac"); m_init_done = true; } //Install the class embedded settings on the nodes in this node container. ns3::NetDeviceContainer BaseWifi::Install(ns3::NodeContainer &nc) { return m_wifiHelper.Install(m_wifiPhyHelper, m_wifiMacHelper, nc); } 
+4
source share
2 answers

I used to encounter such problems. The initialization of the static member objects seems to be very dependent on where the implementation is performed in your code and (possibly) how all this is compiled. The solution I found (somewhere) for this problem was to turn it all into a static member function, for example:

 //in Agent.h class Agent : public ns3::Object{ private: //... static BaseWifi& m_wifi(); //... }; 

and

 //in Agent.cpp BaseWifi& Agent::m_wifi() { static BaseWifi TheObject=BaseWifi(); return TheObject; } 

Thus, the object is initialized correctly the first time a static member function is called.

+5
source

The difference here is that the first approach is to use the copy constructor to initialize the object, while the second uses the default constructor.

0
source

Source: https://habr.com/ru/post/1496779/


All Articles