[Ns-developers] How to initialize an attribute?
Adrian Sai-Wah TAM
adrian at ieaa.org
Fri May 29 11:51:40 PDT 2009
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.
More information about the Ns-developers
mailing list