assignment operator for static array inside a class

This is a discussion on assignment operator for static array inside a class within the C++ Programming forums, part of the General Programming Boards category; Hi I have the following code (shown below) which is a class brt_node with 3 members, one of them being ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    assignment operator for static array inside a class

    Hi I have the following code (shown below) which is a class brt_node with 3 members, one of them being an static array called "buffer", the elements of this buffer are pairs (int, CDataType), the CDataType is also shown.

    I have created my own copy constructors and assignment operators, so that when I do something like the in following lines, I don't have to copy all the buffer, but only the part I am interested in, and given by the function get_buffer_size()

    Code:
    brt_node x;
    //... do something with x
    brt_node n = x; // copy constructor
    brt_node y;
    y = n; // assignment operator
    I know that executing the lines above will work (without my own copy constructor and assigment operator), c++ takes care of it, but is very slow (I don't need to have the whole buffer copied);
    The copy constructor and assigment operator I have defined, do not seem to work properly (segmentation fault sometimes);

    Btw, the array has to be static, don't want a dynamic array in this specific case.

    I would appreciate some help fixing it. Thanks in advance.

    Code:
    class CDataType{
    public:
      // constructors:
      CDataType():
        vertex(0), 
        number(0){}
        
      CDataType(const unsigned int &_v, const cases &_status = unexplored, const unsigned int &_number = 0):
        vertex(_v), 
        number(_number){}
      
      const CDataType &operator=(const CDataType &e) {
        if ( &e != this){
          this->vertex = e.vertex;
          this->number = e.number;
          return (*this);
        }
      }  
      
      // -- attributes
      unsigned int vertex;
      unsigned int number;
    };
    
    
    // -- type of each record stored in the brt_node's buffer
    typedef std::pair<unsigned int, CDataType> value_type;  
    
    
    class brt_node {
      
    public:
      
      // constructors
      brt_node():
        left(0), two_children(false) 
        { buffer[0] = value_type(0, CDataType(0) ); }; 
      
      brt_node(const value_type &_key_pair):
        left(0), two_children(false) 
        { buffer[0] = _key_pair; }
      
      brt_node(const unsigned &_key):
        left(0), two_children(false) 
        { buffer[0] = value_type(_key, CDataType(0) ); }
      
      // copy constructor
      brt_node(const brt_node &_n):
        left(_n.get_leftChildPos()),
        two_children(_n.has_2_children())
        { std::copy(_n.buffer, _n.buffer+_n.get_buffer_size()+1,buffer); }
      
      // functions
    
      KeyType get_key() const { return buffer[0].first; }  
      void    set_key(const KeyType &_key) { buffer[0] = value_type(_key, CDataType(0)); }
      
      KeyType get_buffer_size() const { return buffer[0].second.vertex; }  
      void    set_buffer_size(const unsigned int &size) {  buffer[0].second.vertex = size; }
      
    
      bool has_2_children() const { return two_children; }    
      void set_has_2_children(const bool &_two_children) { two_children = _two_children; }
      
      unsigned int get_leftChildPos() const { return left; }  
      void         set_leftChildPos(const unsigned &_index) { left = _index; }
        
      
      // -- object assignment operator
      const brt_node &operator=(const brt_node &n) {
        if ( &n != this){
          left         = n.get_leftChildPos();
          two_children = n.has_2_children();
          std::copy(n.buffer, n.buffer+n.get_buffer_size()+1, buffer);
          return (*this);
        }
      }  
      
      value_type buffer[BUFFER_SIZE];
    
    private:
      
      unsigned int left;      
      bool two_children;      
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What does get_buffer_size() return? Is it more than BUFFER_SIZE-1?

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    no, it is less than that.... but I realized that it's silly to do what I want in a copy constructor or assignment operator, I better create a function that does copy only the chunk of the buffer that I want.

    thanks

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    buffer is public, and in addition the public methods lets user muck around in the internal details completely unchecked. This class cannot guarantee any invariants, it's at the mercy of the user. Therefore it is not surprising if things blow up occasionally. (At least some asserts might be in order).

    I don't understand much of it, but the following line is either wrong or you are erring against the common convention how ranges are represented in C++.
    Code:
    std::copy(_n.buffer, _n.buffer+_n.get_buffer_size()+1,buffer); //why +1
    If BUFFER_SIZE is a macro, wouldn't it be better to make it static const member instead (to avoid macro name collisions) or perhaps a template argument?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 03:20 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. An Array of Classes in a different Class
    By mas0nite in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2006, 03:28 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21