[Ns-developers] How to initialize an attribute?

Adrian Sai-Wah TAM adrian at ieaa.org
Fri May 29 12:18:26 PDT 2009


Thanks a lot. I think this shall be included in the manual.

- Adrian.

On Fri, May 29, 2009 at 3:02 PM, Mirko Banchi <mk.banchi at gmail.com> wrote:
> Adrian Sai-Wah TAM ha scritto:
>> Hi,
>>
>> First of all, check the following piece of code:
>>
>> ==== 8< ========================
>> #include "ns3/core-module.h"
>>
>> using namespace ns3;
>> using namespace std;
>>
>> class NewObject : public Object
>> {
>>   public:
>>     static TypeId GetTypeId(void) {
>>       static TypeId tid = TypeId("NewObject")
>>         .SetParent<ObjectBase> ()
>>         .AddAttribute ("X", "Just an integer",
>>                        IntegerValue(99),
>>                        MakeIntegerAccessor (&NewObject::x),
>>                        MakeIntegerChecker<int>());
>>       return tid;
>>     };
>>     int GetX() const { return x; };
>>   private:
>>     int x;
>> };
>>
>> int main (int argc, char *argv[])
>> {
>>   Ptr<NewObject> n = CreateObject<NewObject>();
>>   cout << n->GetX() << endl;
>>   return 0;
>> }
>> ==== >8 ========================
>>
>> This is a very simple program that try to create an object called
>> NewObject and initialize "x" into 99, by assigning it as an attribute.
>> However, it doesn't work as expected. What it shows is like a random
>> junk. Does anyone know what is missing in the above code?
>>
>> Cheers,
>> Adrian.
>>
>
> You have to use NS_OBJECT_ENSURE_REGISTERED (NewObject); macro.
>
> Regards,
>
> Mirko
>
> --
> Mirko Banchi
>
> e-mail:    mk.banchi at gmail.com
> e-mail:    mk.banchi at virgilio.it
> id-jabber: mk.banchi at jabber.org
> id-msn:    mb11684 at hotmail.com
>
> PGP key fingerprint:
>
> 308F BFB1 4E67 2522 C88E
> DC69 7631 52ED 32A5 6456
>
>



More information about the Ns-developers mailing list