optional class

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • optional class

      Hey guys, I had a few questions regarding the implementation of the (extremely cool) optional template class.

      First of all, I see that the data itself is stored in the m_data member of the optional_base class. This is essentially a byte array (char's) that the stores the object's data. This data is then cast to the appropriate type whenever needed via the GetT() function. My question is, why it implemented this way instead of using a template type?

      Secondly, I see a lot of stuff like this:

      Source Code

      1. T const * const GetT() const { return reinterpret_cast<T const * const>(m_data); }

      What's up with all those const's? I've never seen them used like that before.

      Finally, I created a very simple class that allocates memory in the constructor and releases it in the destructor. When I use this class as the template parameter for my optional object, I end up getting the destructor called too soon. Here's an example to show you what I mean:

      Brainfuck Source Code

      1. #define DATA_SIZE 50
      2. class tile
      3. {
      4. int x, y;
      5. char *data;
      6. public:
      7. tile(void);
      8. ~tile(void) {if (data) {delete data;}}
      9. //implementation
      10. void draw(void); //simply prints the string right now
      11. //accessors
      12. //...
      13. //(pretend there are accessor functions here)
      14. //...
      15. }; //end class tile
      16. //-----------------------------------
      17. tile::tile(void)
      18. {
      19. data = new char[DATA_SIZE];
      20. memset(data,0,DATA_SIZE);
      21. x = 0; y = 0;
      22. } //end tile::tile()
      23. //-----------------------------------
      24. optional<tile> do_stuff(void)
      25. {
      26. optional<tile> ret;
      27. tile temp; //temp var; very ugly...
      28. char str[DATA_SIZE];
      29. //get a string and store it
      30. cout << "\nwhat string? ";
      31. cin >> str;
      32. temp = str;
      33. ret = temp;
      34. //****************************
      35. //When this function returns, tile::~tile() is called and kills tile::data
      36. //****************************
      37. return ret;
      38. } //end do_stuff()
      39. //-----------------------------------
      40. int main(void)
      41. {
      42. optional<tile> ret;
      43. ret = do_stuff();
      44. if (ret.valid()) {cout << "\n\n"valid\n";}
      45. else {cout << "\n\ninvalid\n";}
      46. } //end main()
      Display All


      Thanks in advance for any help you can provide :)
    • RE: optional class

      Originally posted by rezination

      Secondly, I see a lot of stuff like this:

      Source Code

      1. T const * const GetT() const { return reinterpret_cast<T const * const>(m_data); }

      What's up with all those const's? I've never seen them used like that before.



      In C++ anything that is preceeded by "const" the compiler doesn't let you mess with. Which means that if you say T const * then you can't change the address of T (That is as far as i know.The const thing can get weird in c++)

      In Your code there were a few discrepancies.

      Brainfuck Source Code

      1. //-----------------------------------
      2. optional<tile> do_stuff(void)
      3. {
      4. optional<tile> ret;
      5. tile temp; //temp var; very ugly...
      6. char str[DATA_SIZE];
      7. //get a string and store it
      8. cout << "\nwhat string? ";
      9. cin >> str;
      10. // can str equal temp?
      11. // try defining temp as an optional<tile>
      12. // I dont think it's using optional's overloaded = operator
      13. temp = str;
      14. ret = temp;
      15. //****************************
      16. //When this function returns, tile::~tile() is called and kills tile::data
      17. //Are you sure it just doesnt corrupt something?
      18. //****************************
      19. return ret;
      20. } //end do_stuff()
      21. //-----------------------------------
      22. int main(void)
      23. {
      24. optional<tile> ret;
      25. ret = do_stuff();
      26. if (ret.valid()) {cout << "\n\n"valid\n";}
      27. else {cout << "\n\ninvalid\n";}
      28. } //end main()
      Display All


      I may just not understand what you are trying to do. But as far as i can figure your data is being currupted rather than the destructor being called(but I would think the compiler would give you some kind of error)

      If i'm wrong then, oh well.
      .Code
      push you ; haha!

      The post was edited 1 time, last by DarkPenguin ().

    • RE: optional class

      think of const as meaning this shouldn't be modified.
      the depth of const enforcement can vary from compiler to compiler in my experience.

      A const in a declaration can be used as an optimization hint to the compiler's code generator. One rule to use if you have to cast away the const status of something to pass it as a parameter or something you shouldn't declare it const to avoid giving mis-information to the code generator

      The post was edited 1 time, last by garbob ().

    • RE: optional class

      I've only ever seen const used in two formats. The first is in variable declarations like these:

      Source Code

      1. const int num = 42;
      2. const char *error = "D'oh!";
      3. void set_string(const char *newstring);


      The second is in member functions that don't modify anything:

      Source Code

      1. class lifestuff
      2. {
      3. int life;
      4. public:
      5. int get_life(void) const;
      6. };
      7. int get_life(void) {return life;}


      Does anyone know the meaning of any other usages? Garbob has a really good point about the optimizer, so I certainly want to take advantage of that. :D

      As for the optional template example I used, there's an overloaded assignment operator (=) in the tile class that I ommited. It just performs a strcpy(). I can attach the source code if you like. If I trying to assign str directly to ret, the compiler yells at me since it can't perform the conversion.

      This tile class is just meant to be a test. I'm trying to see if the optional template can be used to deal with objects that use dynamically allocated memory.
    • RE: optional class

      you have pretty much covered the uses i've seen
      one catch to member

      const char *error = "D'oh!";

      this line says i'm creating a pointer to the first character in the character string D'oh! named error and i don't want anyone to be able to change error to point at anything else. it doesn't protect against the string being changed by some part of the program

      I've see many people wonder way a string has been changed when it was declared like that