Created
September 18, 2012 15:16
-
-
Save yifu/3743696 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| // ----------------------------------------------- | |
| // Test the Aggregates propreties with gcc v3.4 | |
| // ----------------------------------------------- | |
| // The rules for a type for being an Aggregates are: | |
| // - No user-defined ctor nor copy ctor. | |
| // - No virtual functions. | |
| // - No private or protected (non static) members or member functions. | |
| // More? | |
| // ----------------------------------------------- | |
| class Agg | |
| { | |
| public: | |
| int a; | |
| int b; | |
| }; | |
| // ----------------------------------------------- | |
| // "More" is also an Aggregate type. | |
| // ----------------------------------------------- | |
| class More | |
| { | |
| public: | |
| int a; | |
| Agg first; | |
| int b; | |
| Agg second; | |
| int c; | |
| }; | |
| // ----------------------------------------------- | |
| // Pretty printer. | |
| // ----------------------------------------------- | |
| ostream& operator << (ostream& os, const Agg& rhs); | |
| // ----------------------------------------------- | |
| // Main function. Holds everything! | |
| // ----------------------------------------------- | |
| int main() | |
| { | |
| cout << "hello world." << endl; | |
| // Aggregates types enables kind of "intialiser list" | |
| // in pre-c++11. | |
| Agg test = { 1, 2 }; | |
| cout << "Agg = " << test << endl; | |
| test.b = 10; | |
| cout << "Agg = " << test << endl; | |
| // test = {88, 99}; // Complier Error! | |
| // test = Agg ({88, 99}); // Complier Error! | |
| const More test2 = {1, {5, 6}, 2, test, 3}; | |
| cout << "More=" << test2.a << ", " | |
| "(" << test2.first << "), " | |
| "" << test2.b << ", " | |
| "(" << test2.second << "), " | |
| "" << test2.c << endl; | |
| } | |
| // ----------------------------------------------- | |
| // Useless part below. | |
| // ----------------------------------------------- | |
| // ----------------------------------------------- | |
| ostream& operator << (ostream& os, const Agg& rhs) | |
| { | |
| return os << rhs.a << "," << rhs.b; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment