explicit help!

Can anyone tell me a bit about this piece of code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class vector
{
  public:
    explicit vector( int theSize = 0 ) : currentSize( theSize )
      { objects = new Object[ currentSize ]; }
    vector( const vector & rhs ) : objects( NULL )
      { operator=( rhs ); }
    ~vector( )
      { delete [ ] objects; }

    int size( ) const
      { return currentSize; }

    Object & operator[]( int index )
    {
                                                     #ifndef NO_CHECK
        if( index < 0 || index >= currentSize )
            throw ArrayIndexOutOfBounds( );
                                                     #endif
        return objects[ index ];
    }

    const Object & operator[]( int index ) const
    {
                                                     #ifndef NO_CHECK
        if( index < 0 || index >= currentSize )
            throw ArrayIndexOutOfBounds( );
                                                     #endif
        return objects[ index ];
    }


    const vector & operator = ( const vector & rhs );
    void resize( int newSize );
  private:
    int currentSize;
    Object * objects;
};


I'm not sure what explicit means and also I'm not sure about const Object & operator[]( int index ) const and every line of code similar to this one. Thanks for the help!

Consider an example
1
2
3
4
struct A{
   int x;
   A(int y) { x = y; }
};
now
1
2
3
int main(){
   A a = 5;
}
would work. If you made A's constructor explicit, you'd be forced to use A a(5);. It may often make sense to allow =, but with std::vector, this is not the case.

If you want to know about operator, see operator overloading in http://www.cplusplus.com/doc/tutorial/classes2/

If you want to know about consts, see http://www.cprogramming.com/tutorial/const_correctness.html
1
2
    explicit vector( int theSize = 0 ) : currentSize( theSize )
      { objects = new Object[ currentSize ]; }

The keyword explicit means that any type conversions to match this signature must be made explicitly. In other words, it prevents implicit conversions. The snippet is constructing a vector to a given size. Now, if the user passed something else (not a size) that can be converted to an int, it will cause a compiler error.

Without the explicit keyword, a user could mistakenly pass something else to this constructor and it would compile. This kind of oversight can cause subtle bugs that can go unnoticed. Off the top of my head (not tested, of course), consider something like this example:
1
2
3
4
5
6
    int * p = new p();
    *p = 10;
    // ...
    vector v( p );
    // ...
    delete p;

In this example, the user meant to pass *p, which is 10, but instead the pointer address is being converted to an int and constructing a huge random sized vector! (Note that this probably wouldn't be an implicit conversion unless the platform's pointer size = int size.)


As for the duplicated code with const's, that is for const correctness. The user of the vector may want to inspect a value read-only rather than modifying it. Such uses (const or not) depend on the context of the call so both cases are available.
Last edited on
Topic archived. No new replies allowed.