Advertisement
fastman92

Class with an error.

Jan 29th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. // Class with error.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. /// Problematic structure
  11. struct sLineTest
  12. {
  13.     enum class objectType
  14.     {
  15.         UNSET_TYPE,
  16.         PLAIN_CONTENT,     
  17.  
  18.         //  Not neccessary for compilation :)
  19.         BLOCK_COMMENT,
  20.  
  21.         UNKNOWN_TYPE,
  22.     };
  23.  
  24.     objectType type;
  25.  
  26.     union
  27.     {
  28.         std::wstring* plainContent;
  29.  
  30.         std::wstring* blockComment;
  31.                
  32.     } content;
  33.  
  34.     void DebugContent(bool showType = false)
  35.     {
  36.         switch(this -> type)
  37.         {
  38.         case objectType::UNSET_TYPE:
  39.             if(showType)
  40.                 std::cout << "{UNSET_TYPE}";
  41.             break;
  42.  
  43.         case objectType::PLAIN_CONTENT:
  44.  
  45.             if(showType)
  46.                 std::cout << "{PLAIN_CONTENT}";
  47.  
  48.             std::wcout << this -> content.plainContent -> c_str();
  49.             break;
  50.         case objectType::BLOCK_COMMENT:
  51.             if(showType)
  52.                 std::cout << "{BLOCK_COMMENT}";
  53.  
  54.                 std::wcout << "/*";
  55.  
  56.             std::wcout << this -> content.blockComment -> c_str();
  57.  
  58.                 std::wcout << "*/";
  59.  
  60.             break;
  61.         default:
  62.             if(showType)
  63.                 std::cout <<"{UNKNOWN:" << (int)this -> type << "}";
  64.         }
  65.     }
  66.  
  67.     void AllocatePlainContent()
  68.     {              
  69.         this -> type = objectType::PLAIN_CONTENT;
  70.  
  71.         this -> content.plainContent = new std::wstring();
  72.     }
  73.  
  74.     void AllocateBlockComment()
  75.     {
  76.         this -> type = objectType::BLOCK_COMMENT;
  77.  
  78.         this -> content.blockComment = new std::wstring();
  79.     }
  80.  
  81.     // Constructor
  82.     sLineTest()
  83.     {
  84.         std::cout << std::endl << "constructor ";
  85.         this -> type = objectType::UNSET_TYPE;
  86.     }
  87.  
  88.     // Copying constructor
  89.     sLineTest(const sLineTest& obj)
  90.     {
  91.         std::cout << std::endl << "copying constructor ";
  92.  
  93.         this -> type = obj.type;
  94.  
  95.         switch(type)
  96.         {
  97.         case objectType::PLAIN_CONTENT:                
  98.             this -> content.plainContent = new std::wstring(*obj.content.plainContent);
  99.             break;
  100.         case objectType::BLOCK_COMMENT:
  101.             this -> content.blockComment = new std::wstring(*obj.content.blockComment);                
  102.             break;
  103.         }
  104.  
  105.         DebugContent(true);
  106.     }
  107.  
  108.  
  109.     void DeallocateObjects()
  110.     {          
  111.         std::cout << std::endl << "destructor " << this << " ";
  112.  
  113.         switch(this -> type)
  114.         {
  115.         case objectType::PLAIN_CONTENT:    
  116.             delete this -> content.plainContent;
  117.             break;
  118.         case objectType::BLOCK_COMMENT:
  119.             delete this -> content.blockComment;
  120.             break;
  121.         }
  122.  
  123.         this -> type = objectType::UNKNOWN_TYPE;
  124.     };
  125.    
  126.     // Destructor
  127.     ~sLineTest()
  128.     {
  129.         // If destruction is executed - you will get a crash
  130.         this -> DeallocateObjects();
  131.     }
  132. };
  133.  
  134. int main(int argc, char* argv[])
  135. {
  136.  
  137.     std::vector<sLineTest> RawLineElements;
  138.    
  139.     RawLineElements.push_back(sLineTest());
  140.     RawLineElements.back().AllocateBlockComment();
  141.     (*RawLineElements.back().content.blockComment) = L"comment!";
  142.     // Deference is neccessary here
  143.     // = operator has to work on wstring, not on wstring*
  144.  
  145.     RawLineElements.push_back(sLineTest());
  146.     RawLineElements.back().AllocatePlainContent();
  147.     (*RawLineElements.back().content.plainContent) = L"plain content!";
  148.    
  149.     cout << endl << "Start ";
  150.    
  151.     cout << &RawLineElements.at(0);
  152.    
  153.     RawLineElements.erase(RawLineElements.begin() + 0);
  154.  
  155.     cout << endl << RawLineElements.size() << " End" << endl;
  156.  
  157.     wcout << endl << RawLineElements.at(0).content.plainContent -> c_str();
  158.  
  159.     cout << RawLineElements.size();
  160.  
  161.     getchar();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement