[英]How to initialize a static member object?
我不知道我不知道這個:)。 這里類似的問題沒有多大幫助。
所以我在這里問。 請考慮以下課程:
//in Agent.h
class Agent : public ns3::Object{
private:
//...
static BaseWifi m_wifi;
//...
};
這是 :
//Agent.cpp
BaseWifi temp;
BaseWifi Agent::m_wifi = temp;
與此截然不同:
//Agent.cpp
BaseWifi Agent::m_wifi = BaseWifi();
第二種方法對我不起作用。 為什么以及如何?
我不想用更多代碼來麻煩你因為我在我的程序中遇到了這個問題。 程序生成seg錯誤,因為BaseWifi
的構造函數中的東西(成員)沒有正確初始化。 當使用那些未初始化的內部成員時,會發生seg錯誤。
提前感謝您的好意見和答案。
ps:事實上,當我還沒有初始化這個靜態成員並且我刪除了一個額外的行時,我發現了這個問題:
BaseWifi temp;
在我的main()
,這增加了我的困惑!!!(這個可能取決於我放在BaseWifi
的構造函數中,所以現在不要介意)
Update-1:對於那些想要看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's 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);
}
我以前遇到過這類問題。 顯然,靜態成員對象的初始化很大程度上取決於代碼中的實現位置以及(可能)整個程序的編譯方式。 我在這個問題上找到的解決方案是將整個事件包裝成一個靜態成員函數,如下所示:
//in Agent.h
class Agent : public ns3::Object{
private:
//...
static BaseWifi& m_wifi();
//...
};
和:
//in Agent.cpp
BaseWifi& Agent::m_wifi() {
static BaseWifi TheObject=BaseWifi();
return TheObject;
}
這樣,在第一次調用靜態成員函數時,對象就會被正確初始化。
這里的區別是第一種方法是使用復制構造函數初始化對象,而第二種方法是使用默認構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.