Skip to content

Instantly share code, notes, and snippets.

@melsov
Last active December 14, 2018 16:51
Show Gist options
  • Save melsov/6f6244113acb35ca528a7adf4f433dc7 to your computer and use it in GitHub Desktop.
Save melsov/6f6244113acb35ca528a7adf4f433dc7 to your computer and use it in GitHub Desktop.
// CppScratchPad.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include <vector>
using namespace std;
class Resource
{
int aNumber;
public:
Resource() : aNumber(3)
{
cout << " Resource constructor " << endl;
}
~Resource()
{
cout << "Resource destructor " << endl;
}
};
class Foo
{
public:
int* a;
int size;
Foo(int _size = 5)
{
cout << "Foo constructor" << endl;
a = new int[size];
size = _size;
}
~Foo()
{
cout << "Foo destructor" << endl;
delete [] a;
}
Foo(const Foo& other)
{
a = new int[other.size];
size = other.size;
std::copy(other.a, other.a + size, a);
//a = other.a;
cout << "Foo copy constructor" << endl;
}
int& operator[](int i)
{
return *(a + i);
}
Foo& operator=(const Foo& other)
{
//tear down
delete[] a;
// copy
a = new int[other.size];
size = other.size;
std::copy(other.a, other.a + size, a);
//a = other.a;
cout << "Foo copy assignment constructor" << endl;
return *this;
}
void DebugFoo()
{
cout << "Foo.a is " << endl;
for (int i = 0; i < size; ++i)
cout << *(a + i) << ", ";
cout << "done" << endl;
}
};
void ErrorsWithFoo()
{
Foo f;
*(f.a + 1) = 3;
f.DebugFoo();
{
Foo g(f);
g.DebugFoo();
{
Foo h;
h = g;
h.DebugFoo();
}
}
}
class Cube
{
public:
int size;
Cube(int _size)
{
size = _size;
}
Cube()
{
size = 0;
}
Cube operator+(const Cube& other)
{
Cube c;
c.size = size + other.size;
return c;
}
};
void ShowTheConstructors()
{
{
Foo f;
Foo g(f);
Foo h;
h = g;
}
cout << "pointers " << endl;
{
Foo* pF = new Foo();
delete pF;
}
}
void AddMany(vector<int>& vec)
{
for (int i = 0; i < 4; ++i)
{
vec.push_back(i);
}
}
void MakeDanglingPointers()
{
vector<int> v;
v.push_back(3);
int *first = &v[0];
AddMany(v);
cout << " first elem is: " << *first << endl;
}
void MessWithTheStack()
{
}
int main()
{
cout << "Hello World!\n";
//ShowTheConstructors();
ErrorsWithFoo();
//MakeDanglingPointers();
//MessWithTheStack();
//Foo f;
//int input;
//cin >> input;
//cout << "f.a " << f.res << endl;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment