标题:返回局部变量或临时变量的地址警告
取消只看楼主
OscarWu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-5-25
结帖率:50%
已结贴  问题点数:20 回复次数:2 
返回局部变量或临时变量的地址警告
各位高手,请帮我分析一下下列代码为何编译时出现“ warning C4172: 返回局部变量或临时变量的地址”,如何修改?谢谢!   
class  DateTime
   {
      public:
         // constructors
         DateTime();
         // destructor
         ~DateTime();

         // manipulators
             DateTime* operator&(DateTime input);
   };

// Operators
DateTime* DateTime::operator&(DateTime input)
{
   return &input;
}
搜索更多相关主题的帖子: warning public return 如何 
2015-05-25 13:57
OscarWu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-5-25
得分:0 
谢谢回复。我想完成下面一些操作:
   typedef struct {
      long        mjd;
      double      fracOfDay;
   }  MJD;
   class  DateTime
   {
      friend ostream &operator<<( ostream &output, DateTime &dt );

      public:
         // constructors
         DateTime();
         DateTime( Time time );

         // destructor
         ~DateTime();

         // initializers
         void  SetTime( Time time );

         // selectors
         GPSTime      GetTime();      
         // manipulators
         const DateTime &operator=(const DateTime &DT2);
     DateTime* operator&(DateTime input);
         DateTime operator + ( const double days );
         double   operator - ( const DateTime &DT2 );
         bool    operator == ( const DateTime &DT2 );
         bool    operator != ( const DateTime &DT2 );
         bool    operator >  ( const DateTime &DT2 );
         bool    operator >= ( const DateTime &DT2 );
         bool    operator <  ( const DateTime &DT2 );
         bool    operator <= ( const DateTime &DT2 );
      private:
         long        mjd;
         double      fractionOfDay;
   };


DateTime* DateTime::operator&(DateTime input)
{
   return &input;
}

// const return avoids: (a1 = a2 ) = a3
const DateTime &DateTime::operator=(const DateTime &DT2)
{
   if( &DT2 != this ) // avoids self assignment
   {
     mjd = DT2.mjd;
     fractionOfDay = DT2.fractionOfDay;
   }
   return *this;
}
2015-05-25 14:55
OscarWu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-5-25
得分:0 
谢谢,没有警告了。
2015-05-26 13:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-445746-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.521348 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved