Created
May 16, 2019 10:48
-
-
Save H2NCH2COOH/baf528c7d3665853ecc5c170f8f0425a to your computer and use it in GitHub Desktop.
C++ constructor example
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
#if 0 | |
g++ -std=c++17 -o a.out -Wall -Wextra $@ $0 || exit | |
./a.out | |
rm -f a.out | |
exit | |
#endif | |
#include <stdio.h> | |
#include <vector> | |
#ifndef NOEXCEPT | |
#define NOEXCEPT noexcept | |
#endif | |
class A | |
{ | |
private: | |
int i; | |
public: | |
A(int i):i(i) | |
{ | |
printf("A{%d} Ctor: %p\n", i, this); | |
} | |
~A() | |
{ | |
printf("A{%d} Dtor: %p\n", i, this); | |
} | |
A(const A& that) NOEXCEPT | |
{ | |
i = that.i; | |
printf("A{%d} Copy: %p <- %p\n", i, this, &that); | |
} | |
A(const A&& that) NOEXCEPT | |
{ | |
i = that.i; | |
printf("A{%d} Move: %p <- %p\n", i, this, &that); | |
} | |
}; | |
int main() | |
{ | |
A temp(1); | |
std::vector<A> v; | |
printf("Push 1\n"); | |
v.push_back(temp); | |
printf("Push 2\n"); | |
v.push_back(A(2)); | |
printf("Exit\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment