![]() |
|
#1
|
|||
|
|||
Vector losing information [on reserve()?]Sorry I've asked a lot of questions lately but heres another one
I am adding "rows" to a 2d vector, and I noticed that periodically I would lose information. I started to display the capacity after each new row is added and found out that it seems to happen everytime that space is implicitly reserved (capacity increases). I'm not sure what to show you, I guess the code that im using to add rows is this: CPP / C++ / C Code:
This makes me think the default constructor is being called on new MapCells, but I don't understand how or why this could happen. When the vector resizes it shouldn't modify any of the existing elements should it ? |
|
#2
|
|||
|
|||
Re: Vector losing information [on reserve()?]Quote:
Ways to avoid the default constructor: 1. Make it private. I usually always do this. Make default constructor, assignment operator and copy constructor private. 2. Implement it. If it's reasonable to construct an object without passing any arguments, then make the default constructor do something useful. 3. "Avoid it." Make a constructor that has default values for all arguments, this prevents the compiler from implementing the default one. What about the constructors in the two classes that are members of the MapCell? The code you showed clearly shows you're calling the default constructor MapCell(). In that respect I don't quite understand what's the mystery part of it. There's probably something I don't understand; in other words, too little information for me and too many questions. __________________
Music, programming, endless learning.. |
|
#3
|
|||
|
|||
Re: Vector losing information [on reserve()?]I think I may have found the problem. If the vector uses the copy constructor for some reason (strange) during reserve() or however it's implemented internally, then that would explain what's happening because my copy constructor is not good, thus here is a new question:
This is the copy constructor: CPP / C++ / C Code:
So the new TerBase(), or new CharBase() is only creating base classes (here is where my information is lost) How can I copy an object when I don't know which child class it is ? |
|
#4
|
|||
|
|||
Re: Vector losing information [on reserve()?]Quote:
Well, it appears you're calling the copy constructors for the classes TerBas and CharBase. I have no idea how you have implemented those. It may also be that in a case like this your compiler uses both the copy constructor and the assignment operator. I have knowledge of neither of those. I have no knowledge of the class hierarchies you're talking about. I have no knowledge if your copy constructors call the base class copy constructors or not. Now, I'm not saying having knowledge of these things would give me the "ahaa's", but the lack of this information doesn't, at least. It would always make things easier if there was something to test and fiddle with. The only I can offer, I guess, is: have you debugged it? Tested the copy constructor? And seen what information is missing after the copy constructing? Quote:
__________________
Music, programming, endless learning.. |
|
#5
|
|||
|
|||
Re: Vector losing information [on reserve()?]Thanks for sticking with me
Sorry I omited a lot of information because I am not sure what information is relavant, so I didn't want to spam 10 pages of code. I didn't know about RTTI before you mentioned it, so I looked it up and read THIS article. The example the author is using there is kind of similar to mine, but he does kind of mention that often there are ways to get around this using polymorphism properly, so im not sure if thats true in my case of not. The problem is simply this: With base classes A and B, and child classes a1, a2,..., an and b1, b2,...,bn MapCell has 2 pointers of type A* and B* When defining the copy constructr for MapCell, in the code I showed last time, I am creating a new A and B, rather then what it should be, a new ax and by, when x and y are some child clases in the passed MapCell. I am really hoping there is a solution withought using typeid and static_cast because like he says in the article, it's kind of a pain when you extend you class hierarchy later. |
|
#6
|
|||
|
|||
Re: Vector losing information [on reserve()?]So I hunted down a c++ TA at school today and discussed the problem with him. Apparently this is a common problem solved with the factory method, creational design pattern.
Most implementations as I see it do indeed use RTTI, or a variation using a local "type" variable for each class. So thanks for the tip, and the help Kimmo The factory method is fairly straightforward so anyone reading this who doesn't know it can check it out HERE |
|
#7
|
|||
|
|||
Re: Vector losing information [on reserve()?]I haven't gone to design patterns much yet, so I wouldn't have known about that. Glad you had someone to ask. And thanks for the links, though I think I'll need a book to fully understand the consepts.
__________________
Music, programming, endless learning.. |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to isolate a piece of information | Richardknox | MySQL / PHP Forum | 1 | 02-Aug-2007 12:15 |
| help : error C2065: 'information' : undeclared identifier | Taichichuan | C Programming Language | 3 | 23-Jan-2007 13:43 |
| Compiled program does not execute | Serpentine | MS Visual C++ / MFC Forum | 1 | 23-Jan-2007 09:41 |
| Computer privacy article | crystalattice | Open Discussion Forum | 0 | 01-Oct-2004 14:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The