// The main module. File: odr_test1.cpp
#include <iostream>
void module1_print(); // declaration of an exeternal function
inline int f1()
{
return 4;
}
class A
{
public:
static double f()
{
return 4.1;
}
};
const double C = 4.2;
constexpr double E = 4.5;
void print()
{
std::cout << "main f1(): " << f1() << std::endl;
std::cout << "main A::f(): " << A::f() << std::endl;
std::cout << "main C: " << C << std::endl;
std::cout << "main E: " << E << std::endl;
}
int main()
{
module1_print();
print();
int i;
std::cin >> i;
}
// File: module1.cpp
#include <iostream>
inline int f1()
{
return 3;
}
class A
{
public:
static double f()
{
return 3.1;
}
};
const double C = 3.2;
constexpr double E = 3.5;
void module1_print()
{
std::cout << "module1 f1(): " << f1() << std::endl;
std::cout << "module1 A::f(): " << A::f() << std::endl;
std::cout << "module1 C: " << C << std::endl;
std::cout << "module1 E: " << E << std::endl;
}

clang++ module1.cpp odr_test1.cpp
运行结果:

若进行下面的编译:
clang++ odr_test1.cpp module1.cpp
则结果如下

g++ module1.cpp odr_test1.cpp -std=c++11

若进行如下编译
g++ odr_test1.cpp module1.cpp -std=c++11

// The main module. File: odr_test2.cpp
#include <iostream>
void module2_print(); // declaration of an external function
namespace
{
inline int f1()
{
return 4;
}
class A
{
public:
static double f()
{
return 4.1;
}
};
}
const double C = 4.2;
constexpr double E = 4.5;
void print()
{
std::cout << "main f1(): " << f1() << std::endl;
std::cout << "main A::f(): " << A::f() << std::endl;
std::cout << "main C: " << C << std::endl;
std::cout << "main E: " << E << std::endl;
}
int main()
{
module2_print();
print();
int i;
std::cin >> i;
}
// File: module2.cpp
#include <iostream>
namespace
{
inline int f1()
{
return 3;
}
class A
{
public:
static double f()
{
return 3.1;
}
};
}
const double C = 3.2;
constexpr double E = 3.5;
void module2_print()
{
std::cout << "module2 f1(): " << f1() << std::endl;
std::cout << "module2 A::f(): " << A::f() << std::endl;
std::cout << "module2 C: " << C << std::endl;
std::cout << "module2 E: " << E << std::endl;
}
运行结果

到此这篇关于C++ odr用法案例详解的文章就介绍到这了,更多相关C++ odr用法内容请搜索html5模板网以前的文章希望大家以后多多支持html5模板网!
浅谈C++ 设计模式的基本原则这篇文章主要介绍了++ 设计模式的基本原则,主要的目标是实现最终目的,高内聚,低耦合,开放封闭原则类的改动
C++基于OpenCV实现手势识别的源码这篇文章主要介绍了C++基于OpenCV手势识别的实现源码,这里用到背景减法模型知识,具体实例代码跟随小编一起看看吧
一道面试题教你轻松玩转C++指针下面小编就为大家带来一篇深入理解c++指针的指针和指针的引用。小编觉得挺不错的,现在就分享给大家,也给大家
一篇文章带你入门C++的异常处理C++ 提供了异常机制,让我们能够捕获运行时错误,本文就详细的介绍了C++异常处理入门,具有一定的参考价值,感兴
C++内存模型和名称空间详解这篇文章主要给大家介绍了关于C/C++中的内存模型和名称空间详解,文中通过示例代码介绍的非常详细,对大家学习或
利用c++写一个简单的推箱子小游戏推箱子想必是很多人童年时期的经典游戏,我们依旧能记得抱个老人机娱乐的场景,下面这篇文章主要给大家介绍了关于