C++ odr用法案例詳解
更新時間:2021年09月13日 11:36:55 作者:會會會飛的魚
這篇文章主要介紹了C++ odr用法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
// 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;
}
1、在VS2017上運行的結(jié)果為:

2、使用clang進行編譯
clang++ module1.cpp odr_test1.cpp
運行結(jié)果:

若進行下面的編譯:
clang++ odr_test1.cpp module1.cpp
則結(jié)果如下

3、使用gcc編譯
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;
}
運行結(jié)果

到此這篇關(guān)于C++ odr用法案例詳解的文章就介紹到這了,更多相關(guān)C++ odr用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言?模擬實現(xiàn)memcpy與memmove函數(shù)詳解
這篇文章主要介紹了C語言詳解如何模擬內(nèi)存函數(shù),用到了mencpy與memmove兩個函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-04-04
c++10進制轉(zhuǎn)換為任意2-16進制數(shù)字的實例
下面小編就為大家?guī)硪黄猚++10進制轉(zhuǎn)換為任意2-16進制數(shù)字的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
c語言使用fdk_aac實現(xiàn)aac音頻解碼為pcm
這篇文章主要為大家詳細介紹了c語言如何使用fdk_aac庫實現(xiàn)aac音頻解碼為pcm的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-11-11

