我正在编写一个库,它有一些依赖项,这些依赖项是通过 CMake 的 FetchContent 引入的。在我的顶层CMakeLists.txt,靠近顶部我有:
include(external/dependencies.cmake)其中包含
include(FetchContent)
# a pretty awesome text formatting library
option(_USE_FMT "include components for formatting helpers based on fmt" ON)
if(_USE_FMT)
FetchContent_Declare(
fmt_fc
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 7.1.3
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(fmt_fc)
endif()
FetchContent_Declare(
eigen_fc
GIT_REPOSITORY https://github.com/eigenteam/eigen-git-mirror.git
GIT_TAG 3.3.7
GIT_SHALLOW TRUE
)
# # do the actual fetching
FetchContent_MakeAvailable(eigen_fc)
Run Code Online (Sandbox Code Playgroud)
所以我有几个依赖项,fmt 和 eigen,其中 fmt 是可选的。后来在顶层CMakeLists.txt我有一个
add_library(my_common "")
add_library(my::my_common ALIAS my_common)
#...
add_subdirectory(src)
add_subdirectory(extras)
Run Code Online (Sandbox Code Playgroud)
在 中src,我有几个target_sources()将文件添加到目标的语句,并且在一个目录中我有一个
target_link_libraries(my_common
PUBLIC
eigen
) …Run Code Online (Sandbox Code Playgroud) 我正在研究一个类(称为D类),它派生自一个类(称为类B),并将由另一个类继承(称为类E).我希望E类的实例能够访问其父级的所有公共函数,但我特别希望防止类E覆盖父类中的任何虚函数.
我认为通过重写D类私有部分中的函数,它会阻止派生类重写,但看起来并非如此.这将编译并运行E类中的函数:
#include <iostream>
//an abstract base class
class b
{
public:
virtual ~b() = default;
protected:
virtual void print() = 0;
virtual void print_two()
{
std::cout << "base class" << std::endl;
}
};
class d: public b
{
public:
virtual ~d() = default;
private:
void print() override // note this is not virtual -adding final here does the trick
{
std::cout << "class d" << std::endl;
}
using b::print_two; //this doesn't work either
};
class e : public d …Run Code Online (Sandbox Code Playgroud)