gpt4 book ai didi

c++ - 为什么下面的代码编译

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:18 25 4
gpt4 key购买 nike

#include<iostream>
using namespace std;
class Foo {
void Bar( void ) const ;
};
int main()
{
Foo f;
cout<<sizeof(f)<<endl;
}

我在 g++ 上运行它,它没有给我任何编译错误。此外,它执行了正确的 o/p 1。但我期待,链接期间出错。这个编译器依赖吗?

最佳答案

我只能想象您会收到一个错误,因为 Foo::Bar 没有定义。标准中的单一定义规则仅要求定义使用过的 元素。在您的特定情况下,您的程序中没有任何内容使用 Foo::Bar,因此该程序不需要该定义。

关于c++ - 为什么下面的代码编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15957124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com