This file contains 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
// Until C++11 | |
template<class T> | |
struct p | |
{ | |
typedef T* Type; | |
}; | |
p<float>::Type y; // y is of type float* | |
// Since C++11: alias template | |
template<class T> |
This file contains 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 <vector> | |
#include <algorithm> | |
int main() { | |
std::vector<int> v = {1, 2, 3, 4, 2, 5, 2, 6}; | |
v.erase(std::remove(std::begin(v), | |
std::end(v), | |
2), | |
std::end(v)); |
This file contains 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
class base { | |
public: | |
virtual ~base() = default; | |
base(const base&) = default; | |
base(base&&) = default; | |
base& operator=(const base&) = default; | |
base& operator=(base&&) = default; | |
}; |
This file contains 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 <string> | |
class foo { | |
public: | |
// Constructor | |
foo(const std::string& arg) : cppstring(arg) {} | |
private: | |
std::string cppstring | |
}; |
This file contains 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 <cstring> | |
#include <iostream> | |
class foo | |
{ | |
public: | |
explicit foo(const char* arg) : cstring{new char[std::strlen(arg)+1]} { | |
std::strcpy(cstring, arg); | |
} | |
~foo() { |
This file contains 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 <cstring> | |
#include <iostream> | |
class foo | |
{ | |
public: | |
// Constructor | |
explicit foo(const char* arg) : cstring{new char[std::strlen(arg)+1]} | |
{ | |
std::strcpy(cstring, arg); |
This file contains 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 "pimpl.h" | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
class foo::impl { | |
public: | |
void initialize() { | |
member1 = "Hello"; |
This file contains 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
#ifndef DOUBLE_LINKED_LIST_H | |
#define DOUBLE_LINKED_LIST_H | |
#include <vector> | |
#include <memory> | |
#include <iostream> | |
template<typename T> | |
struct Node { | |
explicit Node(T val) |
This file contains 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
// unique object ownership | |
auto unique = std::unique_ptr<widget>(new widget()); | |
auto unique = std::make_unique<widget>(); | |
// shared object ownership | |
auto shared = std::make_shared<widget>(); | |
// weak reference to an object managed by std::shared_ptr | |
std::weak_ptr<widget> weak = shared; |
This file contains 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 <cstdio> | |
#include <cstdlib> | |
#include <memory> | |
int main() | |
{ | |
auto file_open = [](const char* filename, const char* mode) -> FILE* | |
{ | |
return std::fopen(filename, mode); | |
}; |
NewerOlder