Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. 23:38] == vicc [c21d8901@gateway/web/freenode/ip.194.29.137.1] has joined #cplusplus.com
  2. [23:38] <vicc> hello, i got a question
  3. [23:39] <Fabtasticwill> ok
  4. [23:39] <Fabtasticwill> What is it?
  5. [23:39] <SGH> write it out
  6. [23:40] <vicc> i have a class graph and i want to define a operator+ which sums two graphs
  7. [23:40] <vicc> adn here's the question
  8. [23:41] <vicc> should operator+ return an object and make on inside a function
  9. [23:42] <vicc> or should operator+ allocate memory by using 'new' and return a refference
  10. [23:42] <SGH> return an object.
  11. [23:42] <SGH> no new's or references.
  12. [23:43] <vicc> but if my class holds a pointer to a stl container (std list o list exactly), should i overload = operator?
  13. [23:43] <charmander> morning
  14. [23:43] <charmander> what did i walk in one
  15. [23:43] <charmander> *on?
  16. [23:44] <charmander> also, i figured out the issue to my multiple os's
  17. [23:44] <SGH> yes
  18. [23:45] <SGH> if you hold any pointer you've allocated with new or delete, you always need to redefine operator= and copyconstructor.
  19. [23:45] <SGH> if possible, also operator=(T&&) and moveconstructor.
  20. [23:45] <SGH> (move assignment ^)
  21. [23:45] <charmander> and ~class()
  22. [23:46] <vicc> so then, what will be the difference between what operator= and constructor does
  23. [23:46] <SGH> btw holding a pointer on its own is a bad idea
  24. [23:46] <SGH> <<copy>> constructor calls operator=
  25. [23:46] <charmander> i think you have it backwards
  26. [23:46] <SGH> nope
  27. [23:46] <charmander> oh wait never mind
  28. [23:46] <SGH> you can't call a constructor from operator= :p
  29. [23:47] <charmander> im thinking of initialization construction
  30. [23:47] <SGH> and operator= should allocate the pointer (if it wasn't already allocated)
  31. [23:47] <SGH> and assign the container themselves (not the pointers!)
  32. [23:47] <SGH> eg
  33. [23:47] <charmander> also, you probably want to use smart pointers vicc
  34. [23:47] <SGH> *m_vector_pointer = *o.m_vector_pointer;
  35. [23:48] <SGH> or you can avoid using pointers in the first places
  36. [23:48] <SGH> container don't weigh a lot, since they internally store two or three pointers
  37. [23:48] <charmander> geordi << sizeof(std::vector<int>)
  38. [23:48] <+geordi> 56
  39. [23:48] <charmander> o.O
  40. [23:49] <SGH> geordi << sizeof(void*)
  41. [23:49] <+geordi> 8
  42. [23:49] <vicc> i think i got confused a bit
  43. [23:49] <charmander> geordi << sizeof(std::vector<int>) - sizeof(int)
  44. [23:49] <+geordi> 52
  45. [23:49] <SGH> geordi << 52/8
  46. [23:49] <+geordi> 6
  47. [23:49] <SGH> 6 pointers there
  48. [23:49] <charmander> i thought it was like that
  49. [23:49] <charmander> also, nice teamwork
  50. [23:49] <SGH> lol
  51. [23:49] <SGH> oops
  52. [23:49] <SGH> 7 pointers*
  53. [23:49] <SGH> it was 56.
  54. [23:50] <SGH> wait wait
  55. [23:50] <SGH> 56 / 8 = ?
  56. [23:50] <SGH> lol
  57. [23:50] <charmander> is 7
  58. [23:50] <SGH> yes, 7.
  59. [23:50] <SGH> @vicc: using pointers to containers is a bad idea
  60. [23:50] <charmander> vicc: ignore this whole thing. SGH was just confirming something
  61. [23:51] <SGH> you should use something like this, which simplifies everything
  62. [23:51] <charmander> i knew it had to be more than 3. there are also iterators and and allocation things and such
  63. [23:51] <SGH> class myClass { std::vector<int> m_mycontainer; };
  64. [23:51] <charmander> ^
  65. [23:51] <SGH> no allocations needed, no copy constructors or anything
  66. [23:51] <SGH> @charmander: usually they only store begin() and size()
  67. [23:51] <SGH> (end() too for std::list, I believe)
  68. [23:52] <SGH> sounds like libc++ keeps some more.
  69. [23:52] <charmander> they also have reverse iterators
  70. [23:52] == Zereo [~quassel@unaffiliated/zereo] has joined #cplusplus.com
  71. [23:52] <SGH> reverse iterators aren't stored separately
  72. [23:52] <SGH> they simply return a proxy class that stores end() and iterates backwards
  73. [23:52] <charmander> hmm
  74. [23:52] == nrd [~nerdux@190.176.193.190] has quit [Quit: Saliendo]
  75. [23:52] <SGH> but, end() is already stored.
  76. [23:52] <SGH> eg
  77. [23:53] == nerdux [~nerdux@unaffiliated/nrd] has joined #cplusplus.com
  78. [23:53] <charmander> i need to find an std::vector implementation one of these days
  79. [23:53] <SGH> template <typename T> class backward_iterator { T iterator; backward_iterator& operator++(){--iterator; return *this; };
  80. [23:53] <SGH> as you see, they simply override operator++ and negate the effects.
  81. [23:53] <SGH> a little bit more overridden operators, but that's the point.
  82. [23:53] <charmander> ah i see
  83. [23:54] <SGH> @vicc: something you didn't get?
  84. [23:54] <SGH> besides my speak with charmander, which is probably more complex than what you may get atm
  85. [23:55] <charmander> ignore everything from: <SGH> container don't weigh a lot, since they internally store two or three pointers
  86. [23:55] <charmander> to: <SGH> yes, 7.
  87. [23:55] <SGH> hmmm. Just found ~28 Kg of "Dinky the T-Rex Souvenirs"
  88. [23:55] <charmander> dibs
  89. [23:55] <SGH> unsurprisingly, cannot move anymore.
  90. [23:56] <vicc> my std::list of pointers to classes may grow signifantly by adding new verticles. Is is still more effective to hold a container in graph class instead a pointer to it?
  91. [23:58] <SGH> As long as you allocate them, they must be stored somewhere. By storing it as a pointer-to-container, you are simply allocating an extra pointer (and adding the overhead to pointer-tracking mechanics).
  92. [23:58] <SGH> So, you are *also* saving space.
  93. [23:59] <vicc> ok, understood
  94. [00:02] <charmander> i need to update my whiteboard :/
  95. [00:02] <SGH> brb dropping 280 Dinky's in the middle of the road.
  96. [00:02] <SGH> ah... it only drops one ;_;
  97. [00:04] <vicc> but, coming back to previous questions, am i thinking right? : to do something like that: Graph c = a + b; (a,b exist) i should make operator+ to return a Graph object which i create inside a function. Then I overload = operator which copies everything from rvalue to lvalue. What about copy constructor then?
  98. [00:05] <SGH> The copy constructor is created by default, and unless you store pointers, you can use that one.
  99. [00:05] <SGH> as for the operator+, its signature should be something on the lines of:
  100. [00:05] <vicc> i do store pointers
  101. [00:05] <SGH> Graph operator= (Graph const& o) const;
  102. [00:06] <computerquip> https://www.youtube.com/watch?v=nDjz5qHIzsc
  103. [00:06] <+usandbots> [ If Anti-Vaccine Parents Rode The Magic School Bus - YouTube ]
  104. [00:06] <SGH> er, operator+ *
  105. [00:06] <SGH> if you do store pointers, your copy constructor should "new" them
  106. [00:06] <SGH> e.g.
  107. [00:06] <SGH> Graph::Graph(Graph const& o) { m_pointer = new PointedType(); *m_pointer = *(o.m_pointer); }
  108. [00:07] <SGH> Or...
  109. [00:07] <SGH> You can first allocate all pointers, then call operator=
  110. [00:07] <SGH> Graph::Graph(Graph const& o) { m_pointer = new PointedType(); m_pointer2 = new PointedType2(); (*this) = o; }
  111. [00:07] <SGH> And your operator= simply copies the pointee values as follows
  112. [00:08] <SGH> Graph& Graph::operator= (Graph const& o) { *m_pointer = *(o.m_pointer); *m_pointer2 = *(o.m_pointer2); return *this; }
  113. [00:08] <SGH> Of course your default constructor must allocate these pointers as well, as follows:
  114. [00:08] <SGH> Graph::Graph() { m_pointer = new PointedType(); m_pointer2 = new PointedType2(); }
  115. [00:09] <SGH> If you care enough, you can initialize them via "initializer lists". Not sure if you know them.
  116. [00:09] <SGH> It simply changes the syntax a bit. They only work with constructors, as follows:
  117. [00:09] <SGH> Graph::Graph() : m_pointer(new PointedType()), m_pointer2(new PointedType2()) {}
  118. [00:10] <SGH> Graph::Graph(Graph const& o) : m_pointer(new PointedType()), m_pointer2(new PointedType2()) {*this = o;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement