Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Class with error.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- using namespace std;
- /// Problematic structure
- struct sLineTest
- {
- enum class objectType
- {
- UNSET_TYPE,
- PLAIN_CONTENT,
- // Not neccessary for compilation :)
- BLOCK_COMMENT,
- UNKNOWN_TYPE,
- };
- objectType type;
- union
- {
- std::wstring* plainContent;
- std::wstring* blockComment;
- } content;
- void DebugContent(bool showType = false)
- {
- switch(this -> type)
- {
- case objectType::UNSET_TYPE:
- if(showType)
- std::cout << "{UNSET_TYPE}";
- break;
- case objectType::PLAIN_CONTENT:
- if(showType)
- std::cout << "{PLAIN_CONTENT}";
- std::wcout << this -> content.plainContent -> c_str();
- break;
- case objectType::BLOCK_COMMENT:
- if(showType)
- std::cout << "{BLOCK_COMMENT}";
- std::wcout << "/*";
- std::wcout << this -> content.blockComment -> c_str();
- std::wcout << "*/";
- break;
- default:
- if(showType)
- std::cout <<"{UNKNOWN:" << (int)this -> type << "}";
- }
- }
- void AllocatePlainContent()
- {
- this -> type = objectType::PLAIN_CONTENT;
- this -> content.plainContent = new std::wstring();
- }
- void AllocateBlockComment()
- {
- this -> type = objectType::BLOCK_COMMENT;
- this -> content.blockComment = new std::wstring();
- }
- // Constructor
- sLineTest()
- {
- std::cout << std::endl << "constructor ";
- this -> type = objectType::UNSET_TYPE;
- }
- // Copying constructor
- sLineTest(const sLineTest& obj)
- {
- std::cout << std::endl << "copying constructor ";
- this -> type = obj.type;
- switch(type)
- {
- case objectType::PLAIN_CONTENT:
- this -> content.plainContent = new std::wstring(*obj.content.plainContent);
- break;
- case objectType::BLOCK_COMMENT:
- this -> content.blockComment = new std::wstring(*obj.content.blockComment);
- break;
- }
- DebugContent(true);
- }
- void DeallocateObjects()
- {
- std::cout << std::endl << "destructor " << this << " ";
- switch(this -> type)
- {
- case objectType::PLAIN_CONTENT:
- delete this -> content.plainContent;
- break;
- case objectType::BLOCK_COMMENT:
- delete this -> content.blockComment;
- break;
- }
- this -> type = objectType::UNKNOWN_TYPE;
- };
- // Destructor
- ~sLineTest()
- {
- // If destruction is executed - you will get a crash
- this -> DeallocateObjects();
- }
- };
- int main(int argc, char* argv[])
- {
- std::vector<sLineTest> RawLineElements;
- RawLineElements.push_back(sLineTest());
- RawLineElements.back().AllocateBlockComment();
- (*RawLineElements.back().content.blockComment) = L"comment!";
- // Deference is neccessary here
- // = operator has to work on wstring, not on wstring*
- RawLineElements.push_back(sLineTest());
- RawLineElements.back().AllocatePlainContent();
- (*RawLineElements.back().content.plainContent) = L"plain content!";
- cout << endl << "Start ";
- cout << &RawLineElements.at(0);
- RawLineElements.erase(RawLineElements.begin() + 0);
- cout << endl << RawLineElements.size() << " End" << endl;
- wcout << endl << RawLineElements.at(0).content.plainContent -> c_str();
- cout << RawLineElements.size();
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement