小编wat*_*son的帖子

C++从具有相同虚函数名的多个基类继承

我试过这段代码:

class A
{
    virtual void foo() = 0;
};

class B
{
    virtual void foo() = 0;
};

class C : public A, public B
{
    //virtual void A::foo(){}
    //virtual void B::foo(){}

    virtual void A::foo();
    virtual void B::foo();
};

void C::A::foo(){}
void C::B::foo(){}

int main()
{
    C c;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用注释部分时可以,但是当我尝试在类声明之外编写定义时,编译器会报告错误.我正在使用MSVC11编译器,有谁知道怎么写这个?我需要将代码移动到cpp文件中.

谢谢~~

c++ virtual class function multiple-inheritance

13
推荐指数
2
解决办法
2万
查看次数

在 C# 8.0 中反序列化对象时处理临时空引用类型?

我正在尝试 C# 8.0,并且我想为整个项目启用空引用检查。我希望我可以改进我的代码设计,并且不会在任何代码范围内禁用可空性上下文。

我在反序列化对象图时遇到了问题。这些对象相互之间有引用,但对于最终用户视图,对象图中的所有引用都必须有一个值。

换句话说,在反序列化过程中,引用可能null,但是在所有对象加载完成后,最终过程会将所有对象链接在一起,从而解析这些null引用。

解决方法

我已经能够使用几种不同的技术来解决这个问题,并且它们都按预期工作。然而,他们还通过引入许多额外的脚手架来显着扩展代码。

选项#1:影子类

例如,我尝试为每种对象编写一个配对类,在反序列化过程中将它们用作中间对象。在这些成对的类中,所有引用都允许为null. 反序列化完成后,我从这些类中复制所有字段并将它们转换为真实对象。当然,使用这种方法,我需要编写很多额外的代码。

选项#2:影子成员

或者,我尝试放置一个可为空的字段和一个不可为空的属性。这与之前的方法类似,但我使用的是成对成员而不是成对类。然后我为每个字段添加一个内部设置器。这种方法的代码比第一种方法少,但它仍然大大增加了我的代码库。

选项#3:反射

传统上,不考虑性能,我会使用反射来管理反序列化,这样每个类几乎没有额外的代码。但是编写自己的解析代码有一些好处——例如,我可以输出更多有用的错误消息,包括有关调用者如何解决问题的提示。

但是当我引入可空字段时,我的解析代码会显着增加——并且其唯一目的是满足代码分析。

示例代码

为了演示,我尽量简化了代码;我的实际课程显然远不止这些。

class Person
{
    private IReadOnlyList<Person>? friends;

    internal Person(string name)
    {
        this.Name = name;
    }

    public string Name { get; }
    public IReadOnlyList<Person> Friends => this.friends!;

    internal SetFriends(IReadOnlyList<Person> friends)
    {
        this.friends = friends;
    }
}

class PersonForSerialize
{
    public string? Name { get; set; }
    public IReadOnlyList<string> Friends { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# .net-core c#-8.0 nullable-reference-types

3
推荐指数
2
解决办法
1555
查看次数

使用gcc4.7在linux x64平台上使用autot和asio测试构建失败

我尝试构建boost asio示例提供的示例代码:http: //www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp11/spawn/echo_server.cpp

我复制所有代码并将其放入cpp文件,使用gcc4.7和cmake在linux上编译,与boost coroutine和boost上下文库链接,但链接失败.

输出如下:

Linking CXX executable ../../../output/bin/unit_test
cd /home/watson/ID_project/build/server_linux_makefile_gcc/abc/test/unit/abc_async && /usr/local/bin/cmake -E cmake_link_script CMakeFiles/unit_test.dir/link.txt --verbose=1
/usr/bin/c++   -std=c++11 -O3 -DNDEBUG  -pthread -lrt -ltcmalloc -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free CMakeFiles/unit_test.dir/TestFileChannel.cpp.o CMakeFiles/unit_test.dir/TestStreamBuffer.cpp.o CMakeFiles/unit_test.dir/TestTimer.cpp.o CMakeFiles/unit_test.dir/TestThreadPool.cpp.o CMakeFiles/unit_test.dir/TestScheduler.cpp.o CMakeFiles/unit_test.dir/PCH.cpp.o CMakeFiles/unit_test.dir/main.cpp.o CMakeFiles/unit_test.dir/TestUDPNetwork.cpp.o CMakeFiles/unit_test.dir/TestTCPNetwork.cpp.o  -o ../../../output/bin/unit_test -rdynamic ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_unit_test_framework-gcc47-mt-1_54.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_context-gcc47-mt-1_54.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_coroutine-gcc47-mt-1_54.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_thread-gcc47-mt-1_54.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_filesystem-gcc47-mt-1_54.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libyaml-cpp.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libmongoc.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_system-gcc47-mt-1_54.a ../../../../../../install/thirdparty_linux_makefile_gcc/lib/libprotobuf.a
../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_coroutine-gcc47-mt-1_54.a(coroutine_context.o): In function `boost::coroutines::detail::coroutine_context::coroutine_context(void (*)(long), boost::coroutines::stack_context*)':
coroutine_context.cpp:(.text+0x103): undefined reference to `make_fcontext'
../../../../../../install/thirdparty_linux_makefile_gcc/lib/libboost_coroutine-gcc47-mt-1_54.a(coroutine_context.o): In function `boost::coroutines::detail::coroutine_context::jump(boost::coroutines::detail::coroutine_context&, long, bool)':
coroutine_context.cpp:(.text+0x1bc): undefined reference to `jump_fcontext'
collect2: error: ld returned 1 exit …
Run Code Online (Sandbox Code Playgroud)

linux boost hyperlink coroutine boost-asio

2
推荐指数
1
解决办法
3475
查看次数