Guest User

Untitled

a guest
Aug 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. HO can you sort a const vector
  2. #include <vector>
  3.  
  4. class Foo
  5. {
  6. public:
  7. // clients can't change this vector directly
  8. const std::vector<int>& get_vector() const { return _vec; }
  9.  
  10. // you can still create an interface that allows
  11. // mutation of the vector in a safe way, or mutate
  12. // the vector internally.
  13. void push_back( int i ) { _vec.push_back( i ); }
  14. private:
  15. std::vector<int> _vec;
  16. }
  17.  
  18. const std::vector< Foo* > v; // hypothetical declaration
  19. std::vector< Foo* >* vPtr = const_cast< std::vector< Foo* >* >(&v);
  20. // GOOD LUCK
  21. (*vPtr)[0] = new Foo();
Add Comment
Please, Sign In to add comment