Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Vector of Vector

Vector of Vector

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicsquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard
    wrote on last edited by
    #1

    Suppose i have got a struct with vectors inside

    struct A
    {
       int a;
       vector b;
    };
    vector  b;
    

    Is this safe or do i have to write a copy contructor ?


    "Just looking for loopholes." W. C. Fields
    American actor, 1880-1946, explaining why he was reading the Bible on his deathbed.

    J 1 Reply Last reply
    0
    • B Bernhard

      Suppose i have got a struct with vectors inside

      struct A
      {
         int a;
         vector b;
      };
      vector  b;
      

      Is this safe or do i have to write a copy contructor ?


      "Just looking for loopholes." W. C. Fields
      American actor, 1880-1946, explaining why he was reading the Bible on his deathbed.

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #2

      Perfectly safe. The compiler will automatically generate a copy constructor using what's defined by the aggregated objects. Generally, a copy constructor is only needed if the structure contains pointers, and aliasing them would be a bad ideatm. Imagine this scenario:

      struct S {
      int* p;
      S() { p = new int[10]; }
      ~S() { delete [] p; }
      };

      {
      S a;
      S b(a);
      } // <- when a and b falls out of scope, the ~S() will crash trying
      // to delete the same array twice.

      In cases like this it's necessary to write your own copy constructor which either a) makes a complete copy of the array inside the object being copied from, or b) you implement some reference counting mechanism. There are gigantic semantic differences between the two cases, as you may understand. It's basically a question of "by value" or "by reference". If I chose b), any modification to let's say a.p[0] would be visible in b.p[0]. In your example, std::vector has a well defined copy constructor. By the way, std::vector implements "by value" semantics. Anyway, the C++ compiler will always automatically generate a copy constructor if, and only if, you omit one. The generated code would look something like this:

      A::A(const A& other) : a(other.a), b(other.b) { /* empty */ }

      . And how a and b are copied is defined by their copy constructors. -- Gott weiß ich will kein Engel sein.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups