Vector class

Pages: 1234
closed account (o1vk4iN6)
Yes the result would be the same but the point is the reallocation would require less memory and time to do so depending on the type.
Last edited on
One shouldn't overlook the fact that move assignment is less likely to throw as it is less likely to be allocating new resources.
It's spam time!

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
57
58
59
60
61
62
#include <algorithm>
#include <iostream>
#include <list>
#include <ostream>

class Whine {
  public:
    Whine()
    {
      std::clog << this << " Whine::Whine()" << std::endl;
    }

    Whine(const Whine &w)
    {
      std::clog << this << " Whine::Whine(const Whine &w) [ &w == " << &w << " ]" << std::endl;
    }

    Whine & operator = (const Whine &w)
    {
      std::clog << this << " Whine::operator=(const Whine &w) [ &w == " << &w << " ]" << std::endl;
      return *this;
    }

    Whine(Whine &&w)
    {
      std::clog << this << " Whine::Whine(Whine &&w) [ &w == " << &w << " ]" << std::endl;
    }

    Whine & operator = (Whine &&w)
    {
      std::clog << this << " Whine::operator=(Whine &&w) [ &w == " << &w << " ]" << std::endl;
      return *this;
    }

    ~Whine()
    {
      std::clog << this << " Whine::~Whine()" << std::endl;
    }
};

int main()
{
  const unsigned long int total = 1;

  const std::list<Whine> copyFrom(total);
  std::list<Whine> copyTo(total);
  std::list<Whine> moveFrom(total);
  std::list<Whine> moveTo(total);

  std::clog << "\ncopy: const copyFrom --> copyTo\n";
  std::copy(copyFrom.begin(), copyFrom.end(), copyTo.begin());

  std::clog << "\nmove: moveFrom --> moveTo\n";
  std::move(moveFrom.begin(), moveFrom.end(), moveTo.begin());

  std::clog << "\nmove: const moveFrom --> moveTo\n";
  std::move(moveFrom.cbegin(), moveFrom.cend(), moveTo.begin());

  std::clog << "\nmove: const copyFrom --> moveTo\n";
  std::move(copyFrom.begin(), copyFrom.end(), moveTo.begin());
  std::clog << '\n';
}

0x18d8020 Whine::Whine()
0x18d8040 Whine::Whine()
0x18d8060 Whine::Whine()
0x18d8080 Whine::Whine()

copy: const copyFrom --> copyTo
0x18d8040 Whine::operator=(const Whine &w) [ &w == 0x18d8020 ]

move: moveFrom --> moveTo
0x18d8080 Whine::operator=(Whine &&w) [ &w == 0x18d8060 ]

move: const moveFrom --> moveTo
0x18d8080 Whine::operator=(const Whine &w) [ &w == 0x18d8060 ]

move: const copyFrom --> moveTo
0x18d8080 Whine::operator=(const Whine &w) [ &w == 0x18d8020 ]

0x18d8080 Whine::~Whine()
0x18d8060 Whine::~Whine()
0x18d8040 Whine::~Whine()
0x18d8020 Whine::~Whine()


Edit: added some newlines.
Last edited on
If that popped up in my spam folder I would file a lawsuit against Yahoo.
Trying to get copy control dealt with here, and I've ran into a problem.

I have an allocator class written up that constructs objects into it's allocated space using that objects copy constructor.

Well, writing the copy constructor for the vector class kind of lead me into this loop. The copy constructor has to copy the underlying dynamic array, but to do this I have to allocated space equal to the capacity of the copied vector. Obviously using new[] will lead me back to the same issue I had earlier of requiring a default ctor. So, do I just need to call alloc.allocate(), supplying the capacity I need, and then call alloc.construct() for each element of the array to be copied? This seems super inefficient, but I can't think of a way around this.
Use operator new to allocate the memory without construction and then placement new to call the copy constructor.
Topic archived. No new replies allowed.
Pages: 1234