考虑以下几点:classDirectoryIterator;namespacedetail{classFileDataProxy;classDirectoryIteratorImpl{friendclassDirectoryIterator;friendclassFileDataProxy;WIN32_FIND_DATAWcurrentData;HANDLEhFind;std::wstringroot;DirectoryIteratorImpl();explicitDirectoryIteratorImpl(conststd::wstring&pathSpec);voidincreme
我遇到了一种奇怪的情况,我的派生类能够访问其涉及模板的基类的私有(private)成员。考虑这个例子:classA{templatestructa{usingtype=a;};};classB:A{templateusingtype=typenamea::type;};intmain(){}编译结果:mingw64/mingw-w64-x86_64-clang3.9.1-3(来自MSYS2)$clang++-Walltest.cpp-otest.exe-std=c++14mingw64/mingw-w64-x86_64-gcc6.3.0-2(来自MSYS2)$g++-Walltest.
使用g++4.2.1编译此代码:structS{};templatestructST{};templateclassref_count:privateBaseType{};templateclassrep_base:publicRefCountType{};classwrap_rep:publicrep_base>{typedefrep_base>base_type;//line11};我得到:bug.cpp:1:error:‘structS’isinaccessiblebug.cpp:11:error:withinthiscontext但是,如果我将wrap_rep类更改为使用ST:
我正在查看我目前工作的公司的一些代码,我在*.cpp文件中看到一些(不是很多)静态全局变量声明(例如,到存储监听器列表),其中.h/.cpp文件属于一个类。如果一个变量(静态或其他)仅由类本身使用,我总是将其声明为私有(private)的。这比声明变量私有(private)有优势吗?这是不好的做法吗?或者当声明仅由类使用而没有其他人使用的静态变量时,这是否正常?EDIT:InmyquestionIaskedaboutstatic,butwhatifitisanon-staticglobalvariableinthe.cppfileinsteadofitbeingaprivatememb
我正在使用广泛使用以下语法的代码库:shared_ptrmyObject=(shared_ptr)newObject();我注意到我无法使用make_shared访问私有(private)构造函数,但是shared_ptrmyObject=(shared_ptr)newObject();工作得很好。我应该仅仅因为它看起来有效而使用它吗?有什么危险吗?它与make_shared有何不同??我知道this中的答案问题,它在make_shared之间进行比较和:std::shared_ptrp2(newObject("foo"));但我没能找到对我遇到的语法的引用。和上面有什么不同,还是一样
基础构造函数的using声明是私有(private)的,但仍然可以构造该类。为什么?对于operator[]的using声明,辅助功能的工作方式不同,必须公开。#includetemplateclassVec:std::vector{private:usingstd::vector::vector;//Works,evenifprivate.Why?public:usingstd::vector::operator[];//mustbepublic};intmain(){Vecvec={2,2};autotest=vec[1];}如果我希望构造函数是私有(private)的怎么办?可以
我见过这样的代码,其中构造函数被声明为私有(private)的,而析构函数是公共(public)的。这样的声明有什么用?析构函数是否需要公开以便在继承期间可以进行调用,还是代码中的错误?这个问题的信息似乎有点少,但我真正想知道的是,当构造函数需要私有(private)时,是否有公共(public)析构函数遵守C++规则? 最佳答案 简答将构造函数创建为私有(private)但将析构函数创建为公共(public)有很多实际用途。您可以使用此范例来:强制引用计数(SeeHiteshVaghani'sexample).实现singleto
我刚才问了一个问题aboutaccessingtheunderlyingcontainerofSTLadapters.我得到了一个非常有帮助的答案:templateS&Container(priority_queue&q){structHackedQueue:privatepriority_queue{staticS&Container(priority_queue&q){returnq.*&HackedQueue::c;}};returnHackedQueue::Container(q);}intmain(){priority_queuepq;vector&tasks=Contain
我有一个resource_manager维护std::vector>的类在内部。resource_manager是resource的好友类.我要resources只能由resource_manager创建/删除,所以我将其构造函数设为私有(private)(可以正常工作)。但是,如果我将析构函数设为私有(private),则代码不会编译,因为析构函数由boost::shared_ptr调用,这不是resource的friend.我正在考虑通过只返回constresource*来强制执行“不要被客户删除”规则来自resource_manager,但不知何故我对这种方法提供的安全性不满意(
我最近从Java和Ruby切换回C++,令我惊讶的是,当我更改私有(private)方法的方法签名时,我不得不重新编译使用公共(public)接口(interface)的文件,因为私有(private)部分也在.h中文件。我很快想出了一个解决方案,我想这对Java程序员来说是典型的:接口(interface)(=纯虚拟基类)。例如:香蕉树.h:classBanana;classBananaTree{public:virtualBanana*getBanana(std::stringconst&name)=0;staticBananaTree*create(std::stringcons