草庐IT

this_date

全部标签

c++ - 为什么 "this"指针用于调用派生成员函数?

在this上阅读关于虚函数的教程(与本例无关)链接,我找到了这段代码。classWeapon{public:voidfeatures(){coutWeapon::features();cout类Weapon派生自类Bomb,其中调用了Weapon的成员函数。为什么使用“this”指针调用函数Weapon::features()?这不是已经隐式给出了吗? 最佳答案 this是隐式给出的,是否显式编写通常是风格问题。在您的情况下,我会说它不会提高可读性。然而,在其他情况下,显式写入this是有意义的,甚至是必要的为了避免局部变量和数据成

c++ - 如何根据 RFC 3339 格式化 boost::date_time-object

我想在boost中使用date_time库来表示我的应用程序中的时间。此应用程序将生成Atom提要,后者又会以RFC3339中指定的格式强制要求时间戳。,例如“1990-12-31T23:59:60Z”或“1990-12-31T15:59:60-08:00”。那么,我该如何根据这个RFC格式化时间呢?我一直在阅读DateTimeInput/Outputdocumentation一整天,我似乎无法找到如何在需要时将Z放在最后。此外,RFC支持可选的小数秒,但只有一位数字(例如“1990-12-31T23:59:60.5Z”)(*)。我似乎也不知道该怎么做。我总是可以编写自己的格式化例程来

c++ - BOOST_FOREACH : What is the error on using this of a STL container?

有谁知道为什么以下会在VC9上产生错误?classElem;classElemVec:publicvector{public:voidfoo();};voidElemVec::foo(){BOOST_FOREACH(Elem&elem,*this){//Dosomethingwithelem}return;}我得到的错误是:errorC2355:'this':canonlybereferencedinsidenon-staticmemberfunctions我现在拥有的唯一(hack)解决方案是:voidElemVec::foo(){ElemVec*This=this;BOOST_FO

c++ - 模板 :Name resolution:Point of instantiation: -->can any one tell some more examples for this statement?

这是来自ISOC++标准14.6.4.1实例化点的声明Forafunctiontemplatespecialization,amemberfunctiontemplatespecialization,oraspecializationforamemberfunctionorstaticdatamemberofaclasstemplate,ifthespecializationisimplicitlyinstantiatedbecauseitisreferencedfromwithinanothertemplatespecializationandthecontextfromwhichi

c++ - 具有固定实现的虚函数不使用 (*this) 的大多数派生类

假设我有以下代码:structZ;structA{virtualvoidDo(Z&z)const;};structB:publicA{};structZ{voiduse(Aconst&a){}voiduse(Bconst&b){}};voidA::Do(Z&z)const{z.use(*this);}现在,当我调用B.do,this的类型是A,这是有道理的,因为do的实现在A中定义.有什么方法可以调用B.do使用use(Bconst&)无需为do复制粘贴相同的代码来自A进入B?在我的实际代码中,我有大约15个(并且还在不断增加)派生自某个基类的类,必须为do复制粘贴相同的代码似乎很浪费

c++ - 在基类中调用 shared_from_this() 时的 bad_weak_ptr

我有一个SuperParent类,一个Parent类(派生自SuperParent)并且都包含一个shared_ptr到一个Child类(它包含一个weak_ptr到一个SuperParent)。不幸的是,我在尝试设置Child的指针时遇到了bad_weak_ptr异常。代码如下:#include#include#include#includeusingnamespaceboost;classSuperParent;classChild{public:voidSetParent(shared_ptrparent){parent_=parent;}private:weak_ptrpare

c++ - 异常 : bad_weak_ptr while shared_from_this

当我这样做时出现异常:std::bad_weak_ptr->shared_from_this()templateclasspainter_record_t{.......private:std::shared_ptr_owner;}这里我想在构造函数中设置“问题”对象:templateclassstream_record_t:publicpainter_record_t{public:stream_record_t(std::shared_ptrowner):painter_record_t(owner){//...}}我有基类:classi_painter_t{public:virt

c++ - 使用 Boost.Date_Time 解析带时区的日期时间?

我想使用BoostDateTimeIO解析带时区的日期时间图书馆。#include#include#includeusingnamespaceboost::gregorian;usingnamespaceboost::posix_time;std::chrono::system_clock::time_pointParseDate(conststd::wstring&dateText,constwchar_t*constformat){ptimetime;std::wstringstreambuffer(dateText);buffer.imbue(std::locale(std::l

c++ - 返回 *this 和 this 之间的区别 - C++

ClassA{//Code...A&operator++(){//code..return____;}Aoperator++(){//code..return___;}我什么时候应该返回*this或this?我理解this是一个指针,*this是指针的取消引用,但是当函数需要通过引用或值来获取值时,我无法决定返回什么。 最佳答案 this在您的函数中属于A*类型,因此返回该类型不合适。*this在您的函数中属于A&类型,它可以绑定(bind)到A&或A。当重载前缀++运算符时,返回A&是函数的正常返回类型。

c++ - type_info 不考虑 cv 限定符 : is this right?

这段代码打印1是正确的行为还是g++4.5的怪癖?#include#includeusingnamespacestd;intmain(){structA{};cout我认为cv限定符的不同类型作为非常不同的类型受到威胁,即使较少的cv限定类型可以隐式转换为更多cv限定的类型。 最佳答案 typeid根据C++标准(摘自ISO/IEC14882:2003的§5.2.8)忽略cv限定符:Thetop-levelcv-qualifiersofthelvalueexpressionorthetype-idthatistheoperandof