C++ odr用法案例詳解
// 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上運(yùn)行的結(jié)果為:

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

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

3、使用gcc編譯
g++ module1.cpp odr_test1.cpp -std=c++11

若進(jìn)行如下編譯
g++ odr_test1.cpp module1.cpp -std=c++11

二、如何解決這個(gè)問(wèn)題
// 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;
}
運(yùn)行結(jié)果

到此這篇關(guān)于C++ odr用法案例詳解的文章就介紹到這了,更多相關(guān)C++ odr用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV使用BSM統(tǒng)計(jì)視頻中移動(dòng)的對(duì)象
這篇文章主要為大家詳細(xì)介紹了OpenCV如何使用BackgroundSubstractor(BSM)實(shí)現(xiàn)視頻中移動(dòng)對(duì)象統(tǒng)計(jì)功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-02-02
C語(yǔ)言?模擬實(shí)現(xiàn)memcpy與memmove函數(shù)詳解
這篇文章主要介紹了C語(yǔ)言詳解如何模擬內(nèi)存函數(shù),用到了mencpy與memmove兩個(gè)函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-04-04
c++10進(jìn)制轉(zhuǎn)換為任意2-16進(jìn)制數(shù)字的實(shí)例
下面小編就為大家?guī)?lái)一篇c++10進(jìn)制轉(zhuǎn)換為任意2-16進(jìn)制數(shù)字的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
c語(yǔ)言使用fdk_aac實(shí)現(xiàn)aac音頻解碼為pcm
這篇文章主要為大家詳細(xì)介紹了c語(yǔ)言如何使用fdk_aac庫(kù)實(shí)現(xiàn)aac音頻解碼為pcm的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
基于list循環(huán)刪除元素,迭代器失效的問(wèn)題詳解
下面小編就為大家?guī)?lái)一篇基于list循環(huán)刪除元素,迭代器失效的問(wèn)題詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
模擬鼠標(biāo)事件的實(shí)現(xiàn)思路及代碼
這篇文章主要介紹了模擬鼠標(biāo)事件的實(shí)現(xiàn)思路及代碼,有需要的朋友可以參考一下2013-12-12

