[英]Are classes allowed to have different definitions across different translation units in a program?
假设类在每个翻译单元中最多定义一次,在不同的翻译单元中以不同的方式定义类是否格式良好?
用例是在没有动态分配的情况下访问实现细节。 C++ 代码将对已由 C 库分配的指针进行操作。
为了举例,请忽略内存泄漏。
通用文件
#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
主程序
#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,并表现出未定义的行为。
[基本.def.odr]
6一个类类型可以有多个定义,[...] 在一个程序中,只要每个定义出现在不同的翻译单元中,并且这些定义满足以下要求。 给定在多个翻译单元中定义的名为 D 的实体,则
- D 的每个定义都应由相同的记号序列组成; 和
- [...]
[...] 如果 D 的定义满足所有这些要求,那么行为就好像只有一个 D 定义。如果 D 的定义不满足这些要求,那么行为是未定义的。
您的两个定义在标记序列上已经很明显不同,因此不支持 ODR 的条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.