如何初始化静态成员对象?(How to initialize a static member object?)

编程入门 行业动态 更新时间:2024-10-22 05:07:17
如何初始化静态成员对象?(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); }

I didn't know that I didn't know this :) . and a similar question here didn't help much.

So here i am asking. Please Consider the following class:

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

is this :

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

very much different from this:

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

The second approach doesn't work for me. why and how?

I don't want to trouble you with more codes coz I faced this problem deep in my program. The program generate seg faults as things(members) inside BaseWifi's constructor are not initialized correctly. when those uninitialized internal members are used, seg faults occur.

Thank you in advance for your kind comments and answers.

p.s.: In fact I found this issue when I hadn't yet initialized this static member and I was deleting an extra line :

BaseWifi temp;

in my main(), which added more to my confusion!!!(this one could be dependent on what I put in BaseWifi's constructor, so dont mind it for now)

Update-1: For those who would like to see the 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; }

这样,在第一次调用静态成员函数时,对象就会被正确初始化。

I have run into these kind of issues before. Apparently the initialization of static member objects very much depends on where the implementation is done in your code and (probably) on how the whole thing is compiled. The solution that I found (somewhere) to the problem was to wrap the whole thing into a static member function like this:

//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; }

This way the object is initialized properly the first time the static member function is called.

更多推荐

本文发布于:2023-07-05 06:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   静态   对象   成员   member

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!