c++ - 存在多态性编译问题的简单文件

标签 c++ polymorphism virtual

<分区>

我在编译一些非常简单的代码时遇到问题,我不知道它有什么问题:/ 看一看:

class A{
public:
    virtual void func() = 0;
};

class B : public A
{
public:
    virtual void func() {};
};

int main()
{
    A* obj = new B();
    return 0;
}

这是我从 g++ 得到的信息:

Info: resolving vtable for _cxxabiv1::__si_class_type_info by 
    linking to __imp __ZTVN10__cxxabiv120__si_class_type_infoE (auto-import) 
Info: resolving vtable for __cxxabiv1::__class_type_info by 
    linking to __imp___Z TVN10__cxxabiv117__class_type_infoE (auto-import) 
k:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: 
warning: auto-importing has been activated without 
    --enable-auto-import specified on the c ommand line. This should work 
    unless it involves constant data structures referencing symbols from 
    auto-imported DLLs

最佳答案

编译没有任何问题,链接有问题(更准确地说,只是警告)。见:

d:\alqualos\pr\testpq>g++ -Wall -c main.cpp
main.cpp: In function 'int main()':
main.cpp:14:8: warning: unused variable 'obj'
d:\alqualos\pr\testpq>g++ -Wall main.o
Info: resolving vtable for __cxxabiv1::__si_class_type_info by linking to __imp_
__ZTVN10__cxxabiv120__si_class_type_infoE (auto-import)
Info: resolving vtable for __cxxabiv1::__class_type_info by linking to __imp___Z
TVN10__cxxabiv117__class_type_infoE (auto-import)
d:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning: a
uto-importing has been activated without --enable-auto-import specified on the c
ommand line.
This should work unless it involves constant data structures referencing symbols
 from auto-imported DLLs.

这是 MinGW 的一个广泛使用的“特性”。如果您的程序需要标准 C++ 库中的任何内容,甚至只是 std::cout,就会发生这种情况。在这种情况下,它与 vtables 有关。要摆脱它:

d:\alqualos\pr\testpq>g++ -Wall -Wl,--enable-auto-import main.o

我不知道“应该工作,除非它涉及引用来自自动导入的 DLL 的符号的常量数据结构”是什么意思。我试过谷歌搜索但没有发现任何有用的东西。如果有人知道它的真正含义以及什么时候可能有危险,请在此处发布答案。

关于c++ - 存在多态性编译问题的简单文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4478090/

相关文章:

c++ - 在 C++ 中,我们如何在不使用友元函数的情况下通过对象调用私有(private)函数?

networking - 如何通过 REST API 添加/编辑虚拟网络

c++ - 异构列表、虚函数和成员数据

c++ - QGraphicsItemGroup::boundingRect() 不更新

c++ - 列表由 C++ 中的模板组成

c++ - 如何在 C 中以函数作为参数调用 Fortran dll

java - 继承不允许我创建子类的对象?

c++ - 有没有办法向这段代码添加按引用传递?

javascript - 多态化将 float 与整数区分开来的函数(javascript)

c# - 反射说接口(interface)方法在实现类型中是虚拟的,而实际上它们不是?