클래스
Inner Class
#include <iostream>
#include <vector>
class Image
{
};
class Inventory
{
private:
class InventoryIcon
{
public:
void Test()
{
Icons;
}
private:
Image Icon;
int Count;
};
std::vector<InventoryIcon> Icons;
};
int main()
{
Inventory Inventory;
}
friend
class A
{
friend class B;
private:
int Value;
};
class B
{
public:
void Test(A& _Other)
{
_Other.Value;
}
private:
void BFunction(A& _Other)
{
_Other.Value;
}
};
class C
{
public:
void Test(A& _Other)
{
_Other.Value;
}
};
자료구조
list
#include <iostream>
#include <Windows.h>
#include <assert.h>
#include <list>
typedef int DataType;
class MyList
{
private:
class ListNode
{
public:
DataType Data = DataType();
ListNode* Next = nullptr;
ListNode* Prev = nullptr;
};
public:
class iterator
{
public:
friend MyList;
iterator()
{
}
iterator(ListNode* _CurNode)
: CurNode(_CurNode)
{
}
bool operator!=(const iterator& _Other)
{
return CurNode != _Other.CurNode;
}
void operator++()
{
CurNode = CurNode->Next;
}
DataType& operator*()
{
return CurNode->Data;
}
private:
ListNode* CurNode = nullptr;
};
public:
MyList()
{
Start->Next = End;
End->Prev = Start;
}
~MyList()
{
ListNode* CurNode = Start;
while (CurNode)
{
ListNode* Next = CurNode->Next;
if (CurNode != nullptr)
{
delete CurNode;
CurNode = Next;
}
}
}
iterator begin()
{
return iterator(Start->Next);
}
iterator end()
{
return iterator(End);
}
void push_back(const DataType& _Data)
{
ListNode* NewNode = new ListNode();
NewNode->Data = _Data;
NewNode->Next = End;
NewNode->Prev = End->Prev;
ListNode* PrevNode = NewNode->Prev;
ListNode* NextNode = NewNode->Next;
PrevNode->Next = NewNode;
NextNode->Prev = NewNode;
}
void push_front(const DataType& _Data)
{
ListNode* NewNode = new ListNode();
NewNode->Data = _Data;
NewNode->Next = Start->Next;
NewNode->Prev = Start;
ListNode* PrevNode = NewNode->Prev;
ListNode* NextNode = NewNode->Next;
PrevNode->Next = NewNode;
NextNode->Prev = NewNode;
}
iterator erase(iterator& _Iter)
{
if (_Iter.CurNode == Start)
{
MessageBoxA(nullptr, "Start를 삭제하려고 했습니다.", "치명적 에러", MB_OK);
assert(false);
}
if (_Iter.CurNode == End)
{
MessageBoxA(nullptr, "End를 삭제하려고 했습니다.", "치명적 에러", MB_OK);
assert(false);
}
iterator ReturnIter;
if (_Iter.CurNode != nullptr)
{
ReturnIter = iterator(_Iter.CurNode->Next);
ListNode* PrevNode = _Iter.CurNode->Prev;
ListNode* NextNode = _Iter.CurNode->Next;
PrevNode->Next = NextNode;
NextNode->Prev = PrevNode;
delete _Iter.CurNode;
_Iter.CurNode = nullptr;
}
return ReturnIter;
}
private:
ListNode* Start = new ListNode();
ListNode* End = new ListNode();
};
int main()
{
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
{
std::cout << "std::list" << std::endl;
std::list<int> NewList = std::list<int>();
for (int i = 0; i < 10; i++)
{
NewList.push_back(i);
}
std::list<int>::iterator StartIter = NewList.begin();
std::list<int>::iterator EndIter = NewList.end();
StartIter = NewList.erase(StartIter);
for (
; StartIter != EndIter; ++StartIter)
{
std::cout << *StartIter << std::endl;
}
}
{
std::cout << "MyList" << std::endl;
MyList NewList = MyList();
for (int i = 0; i < 10; i++)
{
NewList.push_back(i);
}
MyList::iterator StartIter = NewList.begin();
MyList::iterator EndIter = NewList.end();
StartIter = NewList.erase(StartIter);
for (; StartIter != EndIter; ++StartIter)
{
std::cout << *StartIter << std::endl;
}
}
}
과제
240111_RList
#include <iostream>
#include <list>
#include <ConsoleEngine/EngineDebug.h>
typedef int DataType;
class MyList
{
private:
class ListNode
{
public:
DataType Data = DataType();
ListNode* Next = nullptr;
ListNode* Prev = nullptr;
};
class iterator_Base
{
public:
iterator_Base()
{
}
iterator_Base(ListNode* _CurNode)
: CurNode(_CurNode)
{
}
DataType& operator*()
{
return CurNode->Data;
}
bool operator!=(const iterator_Base& _Other)
{
return CurNode != _Other.CurNode;
}
ListNode* CurNode = nullptr;
};
public:
class iterator : public iterator_Base
{
friend MyList;
public:
iterator()
{
}
iterator(ListNode* _CurNode)
: iterator_Base(_CurNode)
{
}
void operator++()
{
CurNode = CurNode->Next;
}
};
class reverse_iterator : public iterator_Base
{
friend MyList;
public:
reverse_iterator()
{
}
reverse_iterator(ListNode* _CurNode)
: iterator_Base(_CurNode)
{
}
void operator++()
{
CurNode = CurNode->Prev;
}
};
MyList()
{
Start->Next = End;
End->Prev = Start;
}
~MyList()
{
ListNode* CurNode = Start;
while (CurNode)
{
ListNode* Next = CurNode->Next;
if (nullptr != CurNode)
{
delete CurNode;
CurNode = Next;
}
}
}
iterator begin()
{
return iterator(Start->Next);
}
iterator end()
{
return iterator(End);
}
reverse_iterator rbegin()
{
return reverse_iterator(End->Prev);
}
reverse_iterator rend()
{
return reverse_iterator(Start);
}
void push_back(const DataType& _Data)
{
ListNode* NewNode = new ListNode();
NewNode->Data = _Data;
NewNode->Next = End;
NewNode->Prev = End->Prev;
ListNode* PrevNode = NewNode->Prev;
ListNode* NextNode = NewNode->Next;
PrevNode->Next = NewNode;
NextNode->Prev = NewNode;
}
protected:
private:
ListNode* Start = new ListNode();
ListNode* End = new ListNode();
};
int main()
{
LeakCheck;
{
std::cout << "std::list" << std::endl;
std::list<int> NewList = std::list<int>();
for (int i = 0; i < 5; i++)
{
NewList.push_back(i);
}
std::list<int>::reverse_iterator rStartIter = NewList.rbegin();
std::list<int>::reverse_iterator rEndIter = NewList.rend();
for (; rStartIter != rEndIter; ++rStartIter)
{
std::cout << *rStartIter << std::endl;
}
}
{
std::cout << "MyList" << std::endl;
MyList NewList = MyList();
for (int i = 0; i < 5; i++)
{
NewList.push_back(i);
}
MyList::reverse_iterator rStartIter = NewList.rbegin();
MyList::reverse_iterator rEndIter = NewList.rend();
for (; rStartIter != rEndIter; ++rStartIter)
{
std::cout << *rStartIter << std::endl;
}
}
}