[Ns-developers] How to initialize an attribute?

Mirko Banchi mk.banchi at gmail.com
Fri May 29 12:02:38 PDT 2009


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3410 bytes
Desc: S/MIME Cryptographic Signature
Url : http://mailman.isi.edu/pipermail/ns-developers/attachments/20090529/d0fe8fbf/smime.bin


More information about the Ns-developers mailing list