gpt4 book ai didi

c++ - 是否允许类在程序中的不同翻译单元中有不同的定义?

转载 作者:行者123 更新时间:2023-12-03 20:08:52 25 4
gpt4 key购买 nike

如果类在每个翻译单元中最多定义一次,那么在不同的翻译单元中定义不同的类是否合式?

用例是在没有动态分配的情况下访问实现细节。 C++ 代码将对已由 C 库分配的指针进行操作。

为了举例,请忽略内存泄漏。

通用.hpp

#pragma once

namespace Test {

class Impl;

class A {
void *ptr;
A(void *ptr) : ptr(ptr) {}
friend class Impl;

public:
int plus_one();
};

class B {
void *ptr;
B(void *ptr) : ptr(ptr) {}
friend class Impl;

public:
int plus_two();
};

class Factory {
public:
A getA(int val);
B getB(int val);
};

} // namespace Test

A.cpp

#include "common.hpp"

namespace Test {

class Impl {
public:
static int as_int(A *a) { return *static_cast<int *>(a->ptr) + 1; }
};

int A::plus_one() { return Impl{}.as_int(this); }

} // namespace Test

B.cpp

#include "common.hpp"

namespace Test {

class Impl {
public:
static int as_int(B *b) { return *static_cast<int *>(b->ptr) + 2; }
};

int B::plus_two() { return Impl{}.as_int(this); }

} // namespace Test

工厂.cpp

#include "common.hpp"

namespace Test {

class Impl {
public:
static A getA(int val) { return A(new int{val}); }
static B getB(int val) { return B(new int{val}); }
};

A Factory::getA(int val) { return Impl{}.getA(val); }
B Factory::getB(int val) { return Impl{}.getB(val); }

} // namespace Test

主要.cpp

#include <iostream>

#include "common.hpp"

int main() {
Test::Factory factory;
std::cout << factory.getA(1).plus_one() << std::endl;
std::cout << factory.getB(1).plus_two() << std::endl;
return 0;
}

输出:

$ g++ A.cpp B.cpp Factory.cpp main.cpp -o test 
$ ./test
2
3

最佳答案

不行,同一个类类型不允许有不同的定义。您的程序直接违反了 ODR,并表现出未定义的行为。

[basic.def.odr]

6 There can be more than one definition of a class type, [...] in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

  • each definition of D shall consist of the same sequence of tokens; and
  • [...]

[...] If the definitions of D satisfy all these requirements, then the behavior is as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.

您的两个定义在它们的 token 序列上已经很明显不同,因此不支持 ODR 的条件。

关于c++ - 是否允许类在程序中的不同翻译单元中有不同的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59657825/

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