草庐IT

base_num

全部标签

c++ - 从 Derived* 到 void* 到 Base* 的 static_cast

我想将指向派生类成员的指针转换为void*,然后从那里转换为基类的指针,如下例所示:#includeclassBase{public:voidfunction1(){std::cout(&d);Base*baseptr=static_cast(ptr);baseptr->function1();baseptr->function2();}这会编译并给出所需的结果(分别打印1和2),但它能保证工作吗?我在这里找到的static_cast的描述:http://en.cppreference.com/w/cpp/language/static_cast只提到转换为void*并返回到指向相同类

c++ - 将私有(private)部分保留在 c++ header 之外 : pure virtual base class vs pimpl

我最近从Java和Ruby切换回C++,令我惊讶的是,当我更改私有(private)方法的方法签名时,我不得不重新编译使用公共(public)接口(interface)的文件,因为私有(private)部分也在.h中文件。我很快想出了一个解决方案,我想这对Java程序员来说是典型的:接口(interface)(=纯虚拟基类)。例如:香蕉树.h:classBanana;classBananaTree{public:virtualBanana*getBanana(std::stringconst&name)=0;staticBananaTree*create(std::stringcons

c++ - 如何在C++中实现继承并解决错误 "parent class is not accessible base of child class"?

我是C++新手。我喜欢探索C++中继承的概念。每当我尝试编译以下代码时,我都会收到错误消息:forC++includes,orinsteadofthedeprecatedheader.Todisablethiswarninguse-Wno-deprecated.D:\CPracticeFiles\Vehicle.cpp:Infunction`intmain()':D:\CPracticeFiles\Vehicle.cpp:26:error:`voidVehicle::setStationary_state(bool)'isinaccessibleD:\CPracticeFiles\Ve

c++: vector<Base> 可以包含 Derived 类型的对象吗?

标题几乎说明了一切。基本上,这样做是否合法:classBase{//stuff}classDerived:publicBase{//morestuff}vectorfoo;Derivedbar;foo.push_back(bar);根据我看过的其他帖子,下面是可以的,但我不想在这种情况下使用指针,因为很难使其线程安全。vectorfoo;Derived*bar=newDerived;foo.push_back(bar); 最佳答案 不,Derived对象将是sliced:所有额外的成员都将被丢弃。使用std::vector>而不是原

C++ 继承 : Calling Base Class Constructor In Header

假设类Child是类Parent的派生类。在一个五文件程序中,我如何在Child.h中指定我想调用Parent的构造函数?我认为header中的以下内容不合法:Child(intParam,intParamTwo):Parent(Param);在这种情况下,Child.cpp的构造函数语法应该是什么样的? 最佳答案 在Child.h中,您只需声明:Child(intParam,intParamTwo);在Child.cpp中,您将拥有:Child::Child(intParam,intParamTwo):Parent(Param){

c++ - 为什么在异常掩码未设置为 eofbit 时 getline() 抛出 'std::ios_base::failure'?

考虑以下代码:ifstreamin;try{in.exceptions(ifstream::failbit|ifstream::badbit);in.open(pConfLocation);}catch(ifstream::failuree){throwstd::runtime_error("Can'topenconfigurationfile\n");}vectorlns;strings;in.clear();while(!in.eof()){getline(in,s);boost::algorithm::trim(s);lns.push_back(s+='\n');}所以:我根据t

c++ - Clang claims that `member reference base type ' X' is not a structure or union`,但 X 是具有推导参数的结构模板

考虑以下代码:templatestructX{X(T){}voidfoo(){}};templatestructY{intobject=0;voidbar(){X(object).foo();}};Liveongcc.godbold.orgGCC8.2编译它,而Clang7吐出以下错误::13:18:error:memberreferencebasetype'X'isnotastructureorunionX(object).foo();~~~~~~~~~^~~~这对我来说像是一个错误。条件非常具体:如果任一结构不是模板,或者object不是成员变量,或者不涉及CTAD(类模板参数推导

c++ - 为什么 num_get 和 num_put 是不对称的?

std::basic_istream的算术提取运算符有non-virtualoverloadsforall8integertypes(不列出字符,它们的处理方式不同),它调用num_get::get,它有个人virtualoverloadsfor6ofthem(缺少short和int的签名版本)std::basic_ostream的算术插入运算符也有non-virtualoverloadsforall8integertypes,它调用num_put::put,它只有virtualoverloadsfor4types,它们是long、longlong及其无符号变体。对于较小的类型,插入运

c++ - 为什么 std::ofstream 在没有 std::ios_base::trunc 的情况下截断?

这个问题在这里已经有了答案:Doesstd::ofstreamtruncateorappendbydefault?(1个回答)关闭2年前。根据此C++引用:http://www.cplusplus.com/reference/fstream/ofstream/ofstream/,std::ofstream的默认打开模式是ios_base::out并且它没有提到隐含的其他模式。因此,我希望如果我用一个小文件覆盖一个大文件,大文件的“超出”部分应该保持不变,只有文件的第一部分应该被新的、更短的数据替换。另一方面,ApacheC++标准库用户指南(http://stdcxx.apache.o

c++ - 实现观察者模式时出现问题 : "Member reference base type ________ is not a structure or union"

我一直在实现准系统观察者模式,但遇到了一个有点神秘的错误:“成员引用基类型‘Observer*’不是结构或union”。我认为这与我对模板的使用有关,我对模板的使用仍然相当不舒服。这是有问题的代码(为了简化事情而删除了大多数缺点/析构函数):主题界面:classSubject{public:virtualvoidnotify();private:listm_observers;};主题实现:voidSubject::notify(){list::iteratori;for(i=m_observers.begin();i!=m_observers.end();i++){*i->updat