Solved

this pointer in Constructor

Posted on 2007-03-23
13
378 Views
Last Modified: 2008-02-01
If in constructor ,i pass "this" pointer to base class constructor . The compiler shows warning
 
        warning C4355: 'this' : used in base member initializer list

How can i avoid this ??
0
Comment
Question by:ratheesh_k
  • 6
  • 5
  • 2
13 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18778456
If you don't dereference the this pointer in the base class constructor, you can ignore the warning.

The issue is that you pass a this pointer for a not yet completely constructed object. Trying to use that this pointer by dereferencing it will invoke undefined behavior - that's why your compiler gives a warning.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18778462
Here's what Microsoft has to say on it :

http://msdn2.microsoft.com/en-us/library/3c594ae3.aspx
0
 

Author Comment

by:ratheesh_k
ID: 18778825
k ,i want to avoid that .... Is there any approach ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18778848
Depends what you're trying to do. Why do you send the this pointer to the base class constructor ? What do you do with it ?

Can you show some code ?
0
 

Author Comment

by:ratheesh_k
ID: 18778871
class TCBlock {

}
************************************************************
typedef  TCBlock *  TCBlockPtr;

******************************************************************
TCBlockPtr spThis;

************************************************************

TCBlock::TCBlock(uint32 srcAddr, uint32 srcPort, uint32 sapi, TCProtocolPtr tcp,
                 uint32 mtuSize)
  : srcAddr(srcAddr),
       spThis(this, NullDeleter()),
   {

  }
***********************************************************************


The ***  shows tat these are from differnt files ...........................
here   spThis(this, NullDeleter()),
       is the concern
0
 

Author Comment

by:ratheesh_k
ID: 18778874
class NullDeleter ;
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 53

Expert Comment

by:Infinity08
ID: 18778908
Can you show a bit more ? ie. show a bit more of class TCBlock (at least the member fields, and the concerned methods).

Can you also tell which files they are, and how these files are interacting ?

And can you tell what base class you were talking about ?

And finally, can you explain what you're trying to do ?
0
 

Author Comment

by:ratheesh_k
ID: 18778946
*********************TCBlock.hpp****************************************
class TCBlock : public TimeListener
{
public:
  //##ModelId=435EA418021F
  class EventItem
  {
  public:
    //##ModelId=435EA6BA01EF
    EventItem(uint32 action, ProtocolEndpointKey key, TCPPacketPtr pkt = TCPPacketPtr())
      : mAction(action),
        mKey(key),
        mPacket(pkt),
        mTimerId(0),
        mItemId(cNextItemId++)
    {
    }

    //##ModelId=436282A400A5
    uint32 getAction() const
    {
      return mAction;
    }

    //##ModelId=435EA7980300
    uint32 getItemId() const
    {
      return mItemId;
    }

    //##ModelId=435EA6F40322
    ProtocolEndpointKey getKey() const
    {
      return mKey;
    }

    //##ModelId=435EA70200CF
    TCPPacketPtr getPacket() const
    {
      return mPacket;
    }

    //##ModelId=437A5C420276
    uint32 getTimerId() const
    {
      return mTimerId;
    }

    //##ModelId=437A5C4A0373
    void setTimerId(uint32 value)
    {
      mTimerId = value;
    }

  private:
    //##ModelId=435EA46503E5
    static uint32 cNextItemId;
    //##ModelId=435EA7A60028
    uint32 mItemId;
    //##ModelId=435EA6600177
    TCPPacketPtr mPacket;
    //##ModelId=435EA67F033B
    ProtocolEndpointKey mKey;
    //##ModelId=43628271018C
    uint32 mAction;
    //##ModelId=437A5C2601C8
    uint32 mTimerId;
  };

  //##ModelId=4362844F0118
  typedef map<uint32, EventItem> EventMap;

  //##ModelId=437420820372
  typedef deque<TCPPacketPtr> PacketQueue;

private:

  friend class FullTCPAgent;

public:
  //##ModelId=42DC3FE50045
  TCBlock(uint32 srcAddr, uint32 srcPort, uint32 sapi, TCProtocolPtr tcp, uint32 mtuSize);

  //##ModelId=435FB62E0346
  TCBlock(const TCBlock& right);

  //##ModelId=42DC3FE50081
  virtual ~TCBlock();

  //##ModelId=435FB64A02C5
  TCBlock& operator =(const TCBlock& right);

  //##ModelId=42DC3FE50083
  void cancelDelayAckTimer();
  //##ModelId=42DC3FE5008B
  void  cancelRtxTimer();
  //##ModelId=42DC3FE5008C
  void  cancelTimers();
  //##ModelId=42DC3FE5008D
  void  close(ProtocolEndpointKey key);
  //##ModelId=42DC3FE5008E
  void  copy4FirstRcvSYN(TCPPacketPtr pkt);
  //##ModelId=42DC3FE5009F
  void  copy4FirstSYN(TCPPacketPtr pkt);
  //##ModelId=42DC3FE500B4
  TCPPacketPtr deepTCPPacketDataCopy(TCPPacketPtr original);
  //##ModelId=42DC3FE500D2
  void delsnd_timer_resched(double time);
  //##ModelId=42DC3FE500DC
  void finish();

  //##ModelId=42DC3FE500E6
  int32 getDelayAckTimerStatus() const
  {
    return delayAckTimerStatus;
  }

  //##ModelId=42DC3FE500E8
  int32 getDestAddr() const
  {
    return destAddr;
  }

  //##ModelId=42DC3FE500F1
  int32 getDestPort() const
  {
    return destPort;
  }

  //##ModelId=42DC3FE500FA
  int32 getMtuSize() const
  {
    return mtuSize;
  }

  //##ModelId=42DC3FE500FC
  int32 getSapi() const
  {
    return sapi;
  }

  //##ModelId=42DC3FE50104
  int32 getSrcAddr() const
  {
    return srcAddr;
  }

  //##ModelId=42DC3FE50106
  int32 getSrcPort() const
  {
    return srcPort;
  }

  //##ModelId=4368E0C002A9
  TCProtocolPtr getTcp()
  {
    return tcp;
  }

  //##ModelId=42DC3FE5010F
  int32 get_delsnd_timer_status() const
  {
    return 0;
  }

  //##ModelId=42DC3FE50111
  int32 get_rtx_timer_status() const
  {
    return timerStatus;
  }

  //##ModelId=42DC3FE50119
  virtual void handleTimeOut(int connectionId, int actionId);
  //##ModelId=42DC3FE50136
  void insertPacketID(TCPPacketPtr pkt);
  //##ModelId=42DC3FE5014A
  void insertRQpck2Map(TCPSegInfoPtr newSegInfo);
  //##ModelId=42DC3FE50154
  void listen(uint32 port);
  //##ModelId=42DC3FE50168
  void newHighestAck(double newAck);
  //##ModelId=42DC3FE5017C
  int32 nextPacketSize();
  //##ModelId=42DC3FE5017D
  int32 nextRetransmitPacketSize(int32 seqNum);
  //##ModelId=42DC3FE50186
  double now();
  //##ModelId=42DC3FE50190
  void open(string& host, uint32 port);
  //##ModelId=42DC3FE501A4
  void printList();
  //##ModelId=42DC3FE501AF
  void printSentPacketQueue();
  //##ModelId=42DC3FE501B0
  void recvBytes(ProtocolEndpointKey key, TCPPacketPtr pkt);
  //##ModelId=42DC3FE500BE
  int32 recvPacket(ProtocolEndpointKey key, TCPPacketPtr pkt);
  //##ModelId=42DC3FE501C3
  int32 rqAdd(int32 start, int32 end, int32 tiflags, TCPPacketPtr pkt);
  //##ModelId=42DC3FE501EB
  void rqClear();
  //##ModelId=42DC3FE501EC
  bool rqEmpty();
  //##ModelId=42DC3FE501FF
  void sendEventToUpperLayer(DataModelEventEnum event);
  //##ModelId=42DC3FE501ED
  void sendPacket(ProtocolEndpointKey key, TCPPacketPtr pkt);
  //##ModelId=42DC3FE50213
  void sendPacket(ProtocolEndpointKey key, TCPPacketPtr pkt, TCPReasonCode reason);
  //##ModelId=42DC3FE50259
  void setDelayAckTimer(ProtocolEndpointKey key);

  //##ModelId=42DC3FE5025A
  void setDestAddr(uint32 value)
  {
    destAddr = value;
  }

  //##ModelId=42DC3FE50263
  void setDestPort(uint32 value)
  {
    destPort = value;
  }

  //##ModelId=42DC3FE50277
  void setSrcAddr(uint32 value)
  {
    srcAddr = value;
  }

  //##ModelId=42DC3FE5028B
  void setSrcPort(uint32 value)
  {
    srcPort = value;
  }

  //##ModelId=42DC3FE5029F
  void setRtxTimer(ProtocolEndpointKey key);

  //##ModelId=42DC3FE502A0
  double uniform(double seed);


private:

  //##ModelId=437A5E86017B
  void cancelEvents(int32 actionId);

  //##ModelId=42DC3FE502B4
  int32 getPacketID() const
  {
    return packetID;
  }

  //##ModelId=42DC3FE502BF
  bool getRunFE() const
  {
    return runFE;
  }

  //##ModelId=436289A200A3
  void registerEvent(double interval, uint32 action, ProtocolEndpointKey key,
                     TCPPacketPtr pkt = TCPPacketPtr());

  //##ModelId=42DC3FE502DC
  void setPacketID(uint32 value)
  {
    packetID = value;
  }

  //##ModelId=42DC3FE502F0
  void setRunFE(bool value)
  {
    runFE = value;
  }

  //##ModelId=43011D020168
  uint32 srcAddr;
  //##ModelId=43011D0202C7
  uint32 destAddr;
  //##ModelId=43011D030034
  uint32 srcPort;
  //##ModelId=43011D03017F
  uint32 destPort;
  //##ModelId=43011D0102C4
  int32 delayAckTimerStatus;
  //##ModelId=43011D020027
  uint32 sapi;
  //##ModelId=42DC3FE40223
  double highestAck;
  //##ModelId=43011D01016F
  int32 timerStatus;
  //##ModelId=42DC3FE4024B
  bool reXmit;
  //##ModelId=42DC3FE4029C
  bool mRcvPktGone2RQ;
  //##ModelId=42DC3FE4038C
  bool overlapPkt;
  //##ModelId=42DC3FE403BF
  bool runFE;
  //##ModelId=43011D0302DE
  uint32 packetID;
  //##ModelId=43011D0401D2
  uint32 mtuSize;
  //##ModelId=435FB5110254
  TCBlockPtr spThis;
  //##ModelId=42DC3FE401CA
  FullTCPAgentPtr tcpWorker;
  //##ModelId=42DC3FE402BB
  TCPPacketPtr mRcvPkt;
  //##ModelId=42DC3FE402C5
  TCPPacketPtr mDummyPkt;
  //##ModelId=42DC3FE403AC
  TCProtocolPtr tcp;
  //##ModelId=435EA7850390
  EventMap mEventMap;
  //##ModelId=42DC3FE40269
  TCPSegInfoMap segInfo2PktMap;
  //##ModelId=42DC3FE40287
  deque<TCPSegInfoPtr> reassembleQueue;
  //##ModelId=42DC3FE50014
  DelayAckStrategyPtr delayAckStrategy;
  //##ModelId=43741DC70190
  PacketQueue mSentPackets;
  //##ModelId=43741E16030D
  PacketQueue mQueuedPackets;
};


***********************************************************************************************

if u want i wll paste cpp file also .
I am trying to remove that waning ,tat i showed earlier

>>>>>>>>>>>And can you tell what base class you were talking about ?
i dont know ....That was the warning ,so i assumed ..

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18779040
>> if u want i wll paste cpp file also .

Don't paste everything - only what's relevant to the problem ... ie. the data members of the classes, as well as the constructors, etc.


>> I am trying to remove that waning ,tat i showed earlier

I mean : what is your code trying to do ?
0
 

Author Comment

by:ratheesh_k
ID: 18791211
>>>>I mean : what is your code trying to do ?

i dont know becos this is a big project ,i was assigned to clear the warning ....
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18792936
How can you make modifications to a project if you don't know what the code is supposed to do ?

Can you still post the relevant code (as I asked earlier) ?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18793776
>>>> Don't paste everything - only what's relevant to the problem
Try to locate class Tvoid TimeListener::anyMember(anytype anyargument)
{
      spThis->anyFunctionOfTCBlock();
      ...
}

imeListener and post the class definition. As far as I could see, it should have a member called 'spThis' which name indicates that it really should store the 'this'. If I am right you should check the implementation of TimeListener where it uses the spThis. If it is not too often you should consider to make the called functions virtual. Then you don't need the spThis but can call the functions from the baseclass without any pointer:

If you make anyFunctionOfTCBlock virtual you can call it without the spThis-> . If you do so with all usge of spThis you can remove it from baseclass.

Regards, Alex

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 50 total points
ID: 18793794
Oh, there was a mixup in my comment:

>>>> Don't paste everything - only what's relevant to the problem
Try to locate class TimeListener and post the class definition. As far as I could see, it should have a member called 'spThis' which name indicates that it really should store the 'this'. If I am right you should check the implementation of TimeListener where it uses the spThis. If it is not too often you should consider to make the called functions virtual. Then you don't need the spThis but can call the functions from the baseclass without any pointer:

void TimeListener::anyMember(anytype anyargument)
{
      spThis->anyFunctionOfTCBlock();
      ...
}

If you make anyFunctionOfTCBlock virtual you can call it without the spThis-> . If you do so with all usage of spThis you can remove it from baseclass.

Regards, Alex
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now