Skip to content

Instantly share code, notes, and snippets.

@lucasdemarchi
Created August 3, 2017 14:42
Show Gist options
  • Save lucasdemarchi/ae63683797a8648739f7c040cc8ead86 to your computer and use it in GitHub Desktop.
Save lucasdemarchi/ae63683797a8648739f7c040cc8ead86 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
// Defining NEW to 1 will trigger the new method of allocating objects
// statically
//#define NEW 1
#define COPY_ERROR 0
class A {
public:
int data;
int data2;
int data3 = 3;
int data4;
#if NEW
static A create() {
return A{};
}
constexpr A(A &&other) = default;
/* Do not allow copies */
A(const A &other) = delete;
A& operator=(const A&) = delete;
private:
#endif
A() : data2(2) { }
};
static A b
#if NEW
= A::create()
#endif
;
int main()
{
printf("%d\n", b.data);
printf("%d\n", b.data2);
printf("%d\n", b.data3);
printf("%d\n", b.data4);
// copy should not be allowed
#if COPY_ERROR
A a = b;
printf("%d\n", b.data);
printf("%d\n", b.data2);
printf("%d\n", b.data3);
printf("%d\n", b.data4);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment