您好,登錄后才能下訂單哦!
這篇文章主要介紹了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; }
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怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。