我有以下类结构:
class A{
A(){}
A(const A& src){}
};
class B : virtual A {
B():A(){}
B(const B& src):A(src){}
};
class C : virtual A {
C():A(){}
C(const C& src):A(src){}
};
class D : virtual B, virtual C {
D():B(),C(){}
D(const D& src):B(src),C(src){}
};
Run Code Online (Sandbox Code Playgroud)
这给了我警告:
在复制构造函数“D”中:
警告:基类“A”应该在复制构造函数中显式初始化
我不理解。D 的复制构造函数调用copy-ctorB 的 B 调用copy-ctorA 的。 为什么它要我调用copy-ctorD 中的 A?
如果我这样做,copy-ctorA 的不会被调用两次吗?一次从 B 调用,一次从 D 调用。
对此的任何投入都非常感谢。
我想在类中使用多索引容器,这取决于类中依赖于模板的类.听起来很复杂,这里是代码:
#include <boost/unordered_map.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
template <typename Type>
class myDataContainer{
public:
struct DataStruct{
double t;
std::vector<Type> data;
};
// indices structs
struct TagTime{};
struct TagOrdered{};
typedef boost::multi_index::multi_index_container<
DataStruct,
boost::multi_index::indexed_by<
boost::multi_index::hashed_unique<boost::multi_index::tag<TagTime>, boost::multi_index::member<DataStruct, double, &DataStruct::t> >,
boost::multi_index::ordered_unique<boost::multi_index::tag<TagOrdered>, boost::multi_index::member<DataStruct, double, &DataStruct::t> > // this index represents timestamp incremental order
>
> InnerDataContainer;
typedef typename boost::multi_index::index<InnerDataContainer,TagTime>::type timestamp_view;
typedef typename boost::multi_index::index<InnerDataContainer,TagOrdered>::type ordered_view;
InnerDataContainer dataContainer;
void begin(){
ordered_view& ordView = dataContainer.get<TagOrdered>();
ordView.begin();
}
};
int main(int argc, char …Run Code Online (Sandbox Code Playgroud) 我有一个包含许多目标的 C++ 项目,其中包括大量 boost 头文件和其他行密集型头文件。大多数目标都包含相同的标头。因此,我认为这可能是使用预编译头(pch)的理想选择。因此,我创建了一个包含最多包含头文件的头文件并对其进行了预编译。
这将编译单元的代码行从 350k 减少到 120k(我将标志传递-save-temps给 gcc 来检查)。我检查了一下,是-H和参数一起使用的,pch前面有一个感叹号。预编译头有550MB。
不过,编译时间仅从 23 秒减少到 20 秒。
预编译头能带来这么小的改进吗?如果不是,我做错了什么?预编译头的编译时间最短的是什么?
编辑:这是 gcc 命令:
/usr/bin/c++
-fPIC -I/projectDir/build/source -I/projectDir/source -I/usr/include/eigen3 -include /projectDir/build/source/Core/core/cotire/Core_ORIGINAL_CXX_prefix.hxx -Winvalid -pch -g -Wall -Wextra -Wno-long-long -Wno-unused-parameter -std=c++0x -DBOOST_ENABLE_ASSERT_HANDLER -D_REENTRANT -o CMakeFiles/SubProject.dir/cotire/SubProject_ORIGINAL_CXX_unity.cxx.o -c /projectDir /build/source/ArmarXCore/statechart/cotire/SubProject_ORIGINAL_CXX_unity.cxx
通过的输出-ftime-report给了我(启用 PCH):
Execution times (seconds)
phase setup : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 0%) wall 1321 kB ( 0%) ggc
phase parsing : 7.29 (32%) usr 1.69 …Run Code Online (Sandbox Code Playgroud)