user2532296
user2532296

Reputation: 848

storing struct variable from function to local struct variable

typenid is a struct that is defined in a class Queue, queue.h

struct typenid{
     typenid() : src_type(0), src_id(0){}
     uint32_t src_type;
     uint32_t src_id;
     } node_details;

The following is a type of class in my event driven simulator.

class FindNextHopEvent : public event {
    public:
    FindNextHopEvent(double time, Packet *packet, Queue::typenid node_details ); 
    ~FindNextHopEvent();
    void process_event();
    Packet *packet;
    Queue::typenid local_node_details; // should get the value of node_details
};

I want to use the node_details struct inside process_event() but I don't want to pass it as a parameter (due to various reason). Is there a way for local_node_details (a struct similar to node_details) to capture the value of node_details,so that I can access it in process_event() ? If so how can i do it. If I declare local_node_details as a pointer then I can use "this" operator but for structs how can I do it.

This is how my current definition looks like.

FindNextHopEvent::FindNextHopEvent(
   double time,
   Packet *packet,
   Queue::typenid node_details
   ): event(NEXT_HOP_EVENT, time) {
 this->packet = packet;
 Queue::typenid local_node_details= node_details;
}

Any help is much appreciated.

Upvotes: 0

Views: 99

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409404

There is a way, by using references. Pass a reference to the structure to the FindNextHopEvent constructor instead, and make your local_node_details structure be a reference, then you can do it:

class FindNextHopEvent : public event {
    Queue::typenid& local_node_details;
    //            ^
    //            |
    // Note use of ampersand to make it a reference

    ...
};

...

//               Note use of ampersand to make it a reference
//                                                          |
//                                                          v
FindNextHopEvent(double time, Packet *packet, Queue::typenid& node_details )
    // Use constructor initializer list to initialize references
    : ..., local_node_details(node_details)
{ ... }

Upvotes: 1

Related Questions