CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2010
    Posts
    7

    Constructor & Destructor

    Hi;

    I've created a child class PF which the base is scheduler class and I need to know a few stuff:

    #define Size 30

    class PFublic scheduler
    {
    protected:
    PfPacketInfo PfPacket[];

    private:
    ....

    Queue* SelectNextQueue(index,&returnIndex);

    }

    1) In the PF constructor, I've initialised as below

    PfPacket[Size] = {NULL};
    is it right?

    2) In the destructor, do I need to delete PfPacket array?
    As far as I know, I should do it for pointer. If yes, how should I delete them.

    3) In the SelectNextQueue(...), I need to copy some parameters into PfPacket[] and then sort the arrays according to the PfPacket.weight. Since the array should contain new parameters or data everytime the SelectNextQueue(..) is called, it is alright if I initialise PfPacketPacket[Size]={NULL) again in the function?


    e.g
    Queue* PF::SelectNextQueue(..)
    {
    PfPacket[Size] = NULL;
    .
    .
    .
    }

    Pls note that I want the PfPacket to be the class member since they are a few member functions that used the values.

    I hope my questions are clear. Sorry for not sharing the codes because they are so long...

    Many thanks,

    RuZee

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor & Destructor

    Code:
    PfPacketInfo PfPacket[];
    This is not C++. C++ does not allow creation of arrays with no size information. You are either programming in another language, or your compiler needs to have ANSI compilation turned on, since that syntax is invalid.
    PfPacket[Size] = {NULL};
    is it right?
    No.
    ) In the destructor, do I need to delete PfPacket array?
    As far as I know, I should do it for pointer. If yes, how should I delete them.
    There is nothing to delete, since the code to "create" this array is invalid and will not compile.

    Learn to use std::vector. That is what standard C++ provides as a "dynamic array". These non-standard C++ hacks are not necessary.
    Code:
    #include <vector>
    //...
    class PF: public scheduler
    {
       protected:
          std::vector<PfPacketInfo> PfPacket;
    
       public:
            PF() : PfPacket(Size) { }
    };
    //...
    Without knowing what PfPacketInfo is (you should post this class), this code does what your invalid code was doing using standard C++, not non-standard extensions.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 25th, 2010 at 02:17 PM.

  3. #3
    Join Date
    Jun 2010
    Posts
    7

    Re: Constructor & Destructor

    TQ Paul McKinzie.

    I'm using a Qualnet network simulator and they claim it to be C++. Whether it is standard or not.I'm not so sure. However, I need to resolve my problems since I customise their codes to my studies. I've changed my codes like this:

    Code:
    PfPacketInfo PfPacket[];


    // /**
    // STRUCT :: ProportionalFairStat
    // DESCRIPTION :: This structure contains statistics for this scheduler
    // **/

    typedef struct
    {
    int packetIndex; // position of the packet
    int queuePriority;
    double currDataRate;
    double currAvgThput;
    double lastAvgThput;
    double ssWeight;
    BOOL skip;
    }PacketInfo;


    // /**
    // CLASS :: ProportionalFairScheduler
    // DESCRIPTION :: Proportional Fair Scheduler Class
    // **/

    class PfScheduler : public Scheduler
    {
    protected:
    ProportionalFairStat* stats;
    PacketInfo PfPacketInfo[PfInfoSize]; < ---- I've defined the size

    public:
    PfScheduler(BOOL enableSchedulerStat,
    const char graphDataStr[]);

    virtual ~PfScheduler();

    virtual void insert(Message* msg,
    BOOL* QueueIsFull,
    const int queueIndex,
    const void* infoField,
    const clocktype currentTime);

    virtual void insert(Message* msg,
    BOOL* QueueIsFull,
    const int queueIndex,
    const void* infoField,
    const clocktype currentTime,
    TosType* tos);

    virtual BOOL retrieve(const int priority,
    const int index,
    Message** msg,
    int* msgPriority,
    const QueueOperation operation,
    const clocktype currentTime);

    virtual int addQueue(Queue* queue,
    const int priority = ALL_PRIORITIES,
    const double weight = 1.0);

    virtual void removeQueue(const int priority);

    virtual void swapQueue(Queue* queue, const int priority);

    virtual void finalize(Node* node,
    const char* layer,
    const int interfaceIndex,
    const char* invokingProtocol = "IP",
    const char* splStatStr = NULL);

    //virtual void SsSortInfo(MacDot16BsSsInfo ssSortInfo[]);

    private:
    // added by rudzi for PF scheduler
    inline Dot16CIDType MacDot16SchPriorityToCid(int priority);
    void SsSortInfo();
    QueueData* PFQueueSchedulerSelectNextQueue(int index, int *returnIndex);
    void UpdateDequeueQueue (int tc, int queuePriority, int queueIndex);
    void UpdateQueue ( int tc, int queuePriority, int queueIndex);
    };PfPacketInfo PfPacket[PfInfoSize];


    I need to initialise my new datatype member in the PFQueueSchdulerSelectNextQueue (..) like this:

    PfPacketInfo[PfInfoSize] = {0}; <---- if this is wrong, do I've to initialise them one by one. How?

    What about my constructor and destructor?


    Pls help me Paul as I've been stuck with these for ages. Hope you could write me the rite codes besides giving me your comments, pls, pls.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor & Destructor

    Please use code tags when posting code.

    Quote Originally Posted by RuZee View Post
    TQ Paul McKinzie.

    I'm using a Qualnet network simulator and they claim it to be C++. Whether it is standard or not.I'm not so sure.
    It is a big deal whether it is standard or not! I don't care what your simulator people "claim", that code you posted is not standard C++. Live with those facts.

    Your whole code is dependent on this bogus syntax. It is not C++, as C++ does not allow creation of arrays with no size information. So the question to ask you is this: What does that syntax mean?
    However, I need to resolve my problems since I customise their codes to my studies. I've changed my codes like this:

    Code:
    PfPacketInfo PfPacket[];
    And still the syntax is wrong.

    I stated that the solution to your problem is to learn how to use std::vector. That is what a dynamic array is in C++. If you learn to use that, then you need not worry about destructor.
    Code:
    #include <vector>
    struct PacketInfo
    {
        int packetIndex;    // position of the packet
        int queuePriority;
        double currDataRate;
        double currAvgThput;
        double lastAvgThput;
        double ssWeight;	
        BOOL skip;
        PacketInfo() : packetIndex(0), queuePriority(0),
                              currDataRate(0.0), currAvgThput(0.0),
                              lastAvgThput(0.0), ssWeight(0.0), 
                              skip(FALSE) { }
    };
    First, if you want to initialize a PacketInfo, write a default constructor for it so that it always is initialized when you create one. Also, "typedef struct" is not necessary if the program is truly a C++ program.

    Second, usage of vector:
    Code:
    #include <vector>
    //...
    class PfScheduler : public Scheduler
    {
        protected:
          std::vector<PacketInfo> PfPacketInfo;    
          //...
       public:
               PfScheduler() : PfPacketInfo(PfInfoSize) 
               {
                  // whatever else
               }
    };
    I didn't look at anything else. There is no need for a destructor, since the vector cleans up all of the memory it allocated to store those PfPacketInfo's.

    Start with this first. This is where you now change the code to be C++ compliant instead of using non-standard C++ hacks, I'll say it again, are totally unnecessary in a C++ program.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2010
    Posts
    7

    Wink Re: Constructor & Destructor

    Hi;

    Thank you for the alternative using [bold]std::vector[bold]. I've implemented it, but not yet compile it due to PC problem. In fact, I've studied it but probably not as depth as you understand it.

    I really wanted to learn from my mistakes what I've done in constructor and destructor, perhaps you could look at my resolution again, pls.

    [code]
    class PfScheduler : public Scheduler
    {
    protected:

    PacketInfo *PfPacketInfo;

    //...
    public:
    // whatever else
    };
    [code]

    At the constructor;
    [code]
    PacketInfo* PfPacketInfo = new PacketInfo[PfInfoSize];
    for (i = 0; i < PfInfoSize; i++)
    {
    PfPacketInfo[i].packetIndex=0; // position of the packet
    PfPacketInfo[i].queuePriority=0;
    PfPacketInfo[i].currDataRate = 0;
    PfPacketInfo[i].currAvgThput=0;
    PfPacketInfoi[i].AvgThput = 0;
    PfPacketInfo[i].ssWeight=0;
    PfPackectInfo[i].skip = FALSE;
    }
    [code]

    At the destructor;
    [code]
    delete []PfPacketInfo;
    [code]

    I got another question on the vector usage:
    [code]
    #include <vector>
    //...
    class PfScheduler : public Scheduler
    {
    protected:
    std::vector<PacketInfo> PfPacketInfo;
    //...
    public:
    PfScheduler() : PfPacketInfo(PfInfoSize)
    {
    // whatever else
    }
    };
    [code]

    What is [bold]PfScheduler():PfPacketInfo(PfInfoSize)[bold] for? Is this a constuctor?

    I could see that it is much easier to use std::vector rather than using constructor and destructor.

    Many thanks again.

    Rudzi

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Constructor & Destructor

    The closing tag is [/code]. Thanks for trying though.

    At the destructor;
    Code:
    delete []PfPacketInfo;
    Correct....sort of. You need to keep in mind that if you implement a custom destructor, then you must also figure out what the copy constructor and operator= need to do (the "rule of three"). In most cases where a custom destructor is needed, including this one, the default versions of those functions are not appropriate.

    You have two basic options: either implement both, or make the class uncopyable by declaring both as private.

    If you use the std::vector approach then it is not necessary to worry about the cctor and assignment operator since vector does that internally.

    What is [bold]PfScheduler():PfPacketInfo(PfInfoSize)[/bold] for? Is this a constuctor?
    It's the PfScheduler constructor, yes. It creates the PfPacketInfo field using an initializer list, which is probably the syntax that's confusing you. Basically it allows you to specify that members of the class should be constructed with something other than their default constructor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured