小编Joh*_*ohn的帖子

c ++ linux双重破坏静态变量.链接符号重叠

环境:linux x64,编译器gcc 4.x

项目有以下结构:

static library "slib"
-- inside this library, there is static object "sobj"

dynamic library "dlib"
-- links statically "slib"

executable "exe":
-- links "slib" statically
-- links "dlib" dynamically
Run Code Online (Sandbox Code Playgroud)

在节目结束时,"sobj"被毁坏两次.这种行为是预期的,但是它在相同的内存地址被破坏了两次,即在析构函数中也是"this" - 因此存在双重破坏问题.我认为这是由于一些符号重叠.

这场冲突的解决方案是什么?也许一些链接选项?


这是测试用例:


main_exe.cpp

#include <cstdlib>

#include "static_lib.h"
#include "dynamic_lib.h"

int main(int argc, char *argv[])
{
    stat_useStatic();
    din_useStatic();
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

static_lib.h

#ifndef STATIC_LIB_H
#define STATIC_LIB_H

#include <cstdio>

void stat_useStatic();
struct CTest
{
    CTest(): status(isAlive)
    {
        printf("CTest() this=%d\n",this);
    }
    ~CTest()
    {
        printf("~CTest() this=%d, %s\n",this,status==isAlive?"is Alive":"is …
Run Code Online (Sandbox Code Playgroud)

c++ linux dynamic-linking static-linking

17
推荐指数
2
解决办法
3538
查看次数

覆盖基类的虚函数,它们不共享公共接口

#include <iostream>
struct B1
{
    virtual void method()=0;
    virtual ~B1(){}
};

struct B2
{
    virtual void method()=0;
    virtual ~B2(){}
};

struct D: B1, B2
{
    virtual void method()
    {
        std::cout << "D::method\n";
    };
};

int main(int argc,char *argv[])
{
    D d;
    B1 &b1=d;
    B2 &b2=d;
    b1.method();
    b2.method();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

注意,B1和B2不共享通用接口.

这合法吗?如果是 - 在哪个标准?C++ 98/03/11?

msvc和gcc都编译好了.

以前我想过,我必须使用一些通用接口来处理这种情况(可能的虚拟继承).

这种情况有什么特别的名字吗?

请问它的工作原理如何?也许一些ISO参考?

c++ virtual overriding multiple-inheritance

13
推荐指数
1
解决办法
3996
查看次数