Having trouble understanding unique constructor syntax

In my textbook, Data Structures and Algorithm Analysis in C++ (Fourth Edition) by Mark Allen Weiss, on page 106, an example is given of a user defined class called Vector. The Vector has three possible different constructors and all three of the constructors contain syntax I am unfamiliar with and that I have been having a difficult time researching. I am also unfamiliar with instances of the class Vector being the data type for "operators".

I know I have a lot of questions below so I don't expect anyone to answer them all. I am just hitting a brick wall with this problem and I need help. Even links to resources that can clarify things for me would be helpful.

Here are my questions:

1) In constructor #1, is "objects" essentially being assigned a new user defined array?

2) In constructor #2, what does it mean for the reference to have Vector as its data type? Is Vector a data type at all in the context of constructor #2's parameter?

3) In constructor #2, what does it mean for "rhs" to take "theSize" and "theCapacity" as members? Is that what is happening with instances "theSize{ rhs.theSize }" and "theCapacity{ rhs.theCapacity }"?

4) In "operator" #1, how is "Vector & operator " assigned a parameter? How can a parameter be assigned to the reference to an operator?

5) In "operator" #1, what does it mean to return "*this"?

6) Why is "objects []" deleted after operator #1?

7) In constructor #3, what does it mean for various members of "rhs" to be assigned values (as done within the brackets of the constructor )?

8) What is the difference between "&" and "&&" when not used as comparison operators?

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <algorithm>

template <typename Object>
class Vector
{
    public:
        //constructor #1
        explicit Vector( int initSize = 0 ): theSize{ initSize }, 
theCapacity{ initSize + SPARE_CAPACITY }
         { objects = new Object[ theCapacity ]; }
 
         //constructor #2
         Vector( const Vector & rhs ) : theSize{ rhs.theSize }, 
theCapacity{ rhs.theCapacity }, objects{ nullptr }
         {
              objects = new Object[ theCapacity ];
              for( int k = 0; k < theSize; ++k )
              {
                  objects[ k ] = rhs.objects[ k ];
              } 

         }

         //operator #1
         Vector & operator= ( const Vector & rhs )
         {
             Vector copy = rhs;
             std::swap( *this, copy );
             return *this;
         }

         ~Vector()
            { delete [ ] objects; }
          
          //constructor #3
          Vector( Vector && rhs ): theSize{ rhs.theSize },
 theCapacity{ rhs.theCapacity }, objects{ rhs. objects }
          {
              rhs.objects = nullptr;
              rhs.theSize = 0;
              rhs.theCapacity = 0;
          }

          //operator #2
          Vector & operator= ( Vector && rhs )
          {
              std::swap( theSize, rhs.theSize );
              std::swap( theCapacity, rhs.theCapacity );
              std::swap( objects, rhs.objects );

              return *this;     
          }
 
          /*the rest of the class are public member
 functions and private member variables- all of which contain syntax I recognize*/
}

Last edited on
1) Yes. Usually it is referred to as a dynamic array because it is dynamically allocated on the heap.

2) Vector will always be a data type. constructor #2 is the copy constructor. It makes an exact copy of the provided Vector data.

3) See 2) It copies the content.

4) The reference (usually pointer) is provided to the assignment operator. The assignment operator is a function like any other but is logically limited to one parameter.

5) The operator accepts a reference to Vector and returns a reference to Vector. *this is implicitely converted to that reference. this is always a pointer to the type of the class/struct and is implicitely/invisibly passed as the first parameter.

6) The ~ symbol denotes a destructor. In order to avoid memory leaks you should delete the dynamic allocated objects there.

7) Again the values and pointers are copied.

8) In this context && invokes the move semantic. In short: You can make shallow copy of the provided object as soon as you leave the provided object in a stable state. That means in this case especially line 39 means that objects is not deleted twice.
1) Objects is a pointer. It is being made to point at the first element of an array, which was created using new.

2) It's a copy constructor. A new Vector is being made by the constructor; it will be a copy of an existing Vector.

Is Vector a data type at all in the context of constructor #2's parameter?
Yes. It is a data type.

3) In constructor #2, what does it mean for "rhs" to take "theSize" and "theCapacity" as members?
rhs is an object of type Vector. Vector has member variables. theSize is one such member variable. theCapacity is another member variable.

4) In "operator" #1, how is "Vector & operator " assigned a parameter? How can a parameter be assigned to the reference to an operator?

You misunderstand the syntax. Here, Vector & operator means that operator is a reference to a Vector.

5) In "operator" #1, what does it mean to return "*this"?
this is a pointer to the object itself. * is the dereference operator. Do you know dereferencing a pointer is?

6) Why is "objects []" deleted after operator #1?

That the destructor.

7) In constructor #3, what does it mean for various members of "rhs" to be assigned values (as done within the brackets of the constructor )?

It means that the members of "rhs" have been assigned values. Do you know what that is? It's like x = y; It sets the value of something. In this case, the valuies of rhs are set to zero because this is the "move" constructor, and after a "move" construction, the source object is basically not to be used again, and any resources it owned are moved to the new object.

8) What is the difference between "&" and "&&" when not used as comparison operators?

After a type, & means reference-to-that-type. In the code above, && means an "rvalue reference". Now you know the words you can search for them.



1. Vector<Object> seems to have member 'objects' that points to an array of Objects. Every Vector always has the array, because you allocate one on constructor. You could initialize the objects in the member initializer list:
1
2
3
4
5
6
explicit Vector( int initSize = 0 )
  : theSize{ initSize }, 
    theCapacity{ initSize + SPARE_CAPACITY },
    objects{ new Object[ theCapacity ] }
{
}


2. The Vector is a typename. The constructor takes a Vector object parameter by reference.
The template type the parameter is implicitly same as of the constructed type.

3. "rhs" is the name of the parameter, just like 'snafu' is in void fubar( int snafu ).
The rhs is a Vector and Vector has members like 'theSize'.

4. An operator is a function. Function can take parameter by reference. Why could it not?

5. The this inside member function is an automatically created pointer that points to the object on which the function is called on. Dereferencing a pointer yields the pointed to object.

6. Destructor's duty is to clean up. To release managed resources
1
2
3
4
~Vector()
{
  delete [ ] objects;
}


8&7: The && is rvalue reference; a syntax added by C++11. Your constructor #3 is move constructor. The content of parameter rhs is moved to the new object. The object state of rhs is assumed to be undefined after the move, but the assignments ensure consistent state that will not cause problems when the destructor of rhs is called.
Thank you all for your input. I still have some questions:

1) For constructor #2, how would you initialize an instance of the copied object in main()? How would you write it out? Would it look like this?:

1
2
3
4
5
6
7
8
9
10
11
12
#include "Vector.h" 
/*the vector class example given is called Vector but 
I don't know what the file name is since it isn't specified*/

int main()
{
    Vector vector<int> ( 5 ) foo; 

    Vector vector<int>( & foo ) bar; 

    return 0;
}

2) For constructor #2, does the rhs object (which is essentially the copy object, right?) take the member variable values of the initial object which is initialized using constructor #1?

3) How would you write out an instance of operator #1 and operator #2 in main()? Would they both look like this?:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Vector.h"

int main()
{
    Vector vector<int> ( 5 ) foo; 
    //operator overloading # 1
    Vector vector<int>( 4 ) & bar = foo;

    //operator overloading # 2
    Vector vector<int>( 3 ) && array = {1, 2, 3};

    return 0;
}

I know that & operator= is used for copying the addresses from the right hand value in to the left hand value, therefore making them synonymous values (thus an lvalue refrence). I also know that && operator= is used for moving the address from the right hand value to the left hand value but is only used for copying literals and temporary values. As you all have said, this leaves the right hand value in an unspecified but managed space.

4) How would you write out the destructor in main() and when would you do it? After objects [] is no longer being used?
Last edited on
Declaration of a variable:
type name initializer


1
2
Vector<double> foo ( 5 ); // calls #1
Vector<double> bar ( foo ); // calls #2 

The Vector<double> is type.
The foo and bar are names of variables.
The (5) and (foo) are initializers, arguments to constructor functions.

Operator= is assignment operator. Not initializer. The = has different meaning in some contexts.

1
2
3
int x = 42; // this is initialization syntax, not assignment
int y;
y = x + 2; // here is assignment 
Topic archived. No new replies allowed.