From e32eb715ef41e411da0a91b40e6e35f150a9c2eb Mon Sep 17 00:00:00 2001 From: Cameron Wood Date: Sun, 11 Oct 2015 03:41:20 -0400 Subject: [C++/en] Small inheritance clarification Clarified that private members are inherited but are not directly accessible --- c++.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 6f4d0562..2bee51dc 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -404,6 +404,8 @@ int main() { // Inheritance: // This class inherits everything public and protected from the Dog class +// as well as private but may not directly access private members/methods +// without a public or protected method for doing so class OwnedDog : public Dog { void setOwner(const std::string& dogsOwner); -- cgit v1.2.3 From ca435fbb0dd09cdc9c70fe945a891ae3e6c19ab2 Mon Sep 17 00:00:00 2001 From: Cameron Wood Date: Sat, 17 Oct 2015 06:05:54 -0400 Subject: [C++/en] Added in Enum information Added in some information about enums in C++ including some different use cases --- c++.html.markdown | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 2bee51dc..d03092e5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -310,6 +310,70 @@ basic_string(basic_string&& other); // constructor that "salvages" parts of that temporary string. You will see this // concept referred to as "move semantics". +///////////////////// +// Enums +///////////////////// + +// Enums are a way to assign a value to a constant most commonly used for +// easier visualization and reading of code +enum ECarTypes +{ + Sedan, + Hatchback, + SUV, + Wagon +}; + +ECarTypes GetPreferredCarType() +{ + return ECarTypes::Hatchback; +} + +// As of C++11 there is an easy way to assign a type to the enum which can be +// useful in serialization of data and converting enums back-and-forth between +// the desired type and their respective constants +enum ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // The enum is implicitly converted to a uint8_t due to its declared enum type + WriteByteToFile(InputCarType); +} + +// On the other hand you may not want enums to be accidentally cast to an integer +// type or to other enums so it is instead possible to create an enum class which +// won't be implicitly converted +enum class ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // Won't compile even though ECarTypes is a uint8_t due to the enum + // being declared as an "enum class"! + WriteByteToFile(InputCarType); +} + ////////////////////////////////////////// // Classes and object-oriented programming ////////////////////////////////////////// -- cgit v1.2.3 From c3e769e4ac50d4475a530969663e073f4ff002ca Mon Sep 17 00:00:00 2001 From: Brook Zhou Date: Sat, 31 Oct 2015 16:17:58 -0700 Subject: [cpp/en] comparator function for std containers --- c++.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index d03092e5..6b452b1b 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -801,6 +801,24 @@ void doSomethingWithAFile(const std::string& filename) // all automatically destroy their contents when they fall out of scope. // - Mutexes using lock_guard and unique_lock +// containers with object keys of non-primitive values (custom classes) require +// compare function in the object itself or as a function pointer. Primitives +// have default comparators, but you can override it. +class Foo { +public: + int j; + Foo(int a) : j(a) {} +}; +struct compareFunction { + bool operator()(const Foo& a, const Foo& b) const { + return a.j < b.j; + } +}; +//this isn't allowed (although it can vary depending on compiler) +//std::map fooMap; +std::map fooMap; +fooMap[Foo(1)] = 1; +fooMap.find(Foo(1)); //true ///////////////////// // Fun stuff -- cgit v1.2.3