Skip to content

Instantly share code, notes, and snippets.

@ryancdavison
Last active July 31, 2020 07:27
Show Gist options
  • Save ryancdavison/551ccc22fe213e1bb1bebba7ab8bb494 to your computer and use it in GitHub Desktop.
Save ryancdavison/551ccc22fe213e1bb1bebba7ab8bb494 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class A {
// no constructors or assignment operators are defined, so using compiler generated ones
public:
void setA(int value) { a = value; }
int getA() { return a; }
void setF(float value) { f = value; }
float getF() { return f; }
private:
int a = -1;
float f = -1.0f;
};
int main()
{
A a; // compiler generated default constructor
std::cout << "values from compiler generated default constructor" << std::endl;
std::cout << "a.a = " << a.getA() << std::endl;
std::cout << "a.f = " << a.getF() << std::endl;
std::cout << std::endl;
a.setA(2);
a.setF(2.0f);
A b(a); // compiler generated copy constructor
std::cout << "values from compiler generated copy constructor" << std::endl;
std::cout << "b.a = " << b.getA() << std::endl;
std::cout << "b.f = " << b.getF() << std::endl;
std::cout << std::endl;
a.setA(3);
a.setF(3.0f);
A c;
c = a; // compiler generated assignment operator
std::cout << "values from compiler generated assignment operator" << std::endl;
std::cout << "c.a = " << c.getA() << std::endl;
std::cout << "c.f = " << c.getF() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment