GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2007, 01:20
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

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:
//Add 1 row
for (UINT i = 0; i < map.size(); i++)
    map[i].push_back(MapCell());
The MapCell class has 2 main componente. 2 dynamically allocated objects of 2 sepperate class hierarchies. Basically whats happening when I say "losing information" is that the 2 internal components to MapCell are becoming their default values.

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  
Old 04-Nov-2007, 02:45
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Vector losing information [on reserve()?]


Quote:
Originally Posted by Algar
This makes me think the default constructor is being called on new MapCells, but I don't understand how or why this could happen.
These are just stabs in the dark, but...

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  
Old 04-Nov-2007, 11:18
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

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:
MapCell::MapCell(const MapCell & mapCell)
{
	terrain = new TerBase(mapCell.getTerrain());
	character = new CharBase(mapCell.getCharacter());
//....
but the .getTerrain() and .getCharacter() return one of many different TerBas and one of many CharBase child classes. The child classes only add member functions that are virtual in the base classes.

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  
Old 04-Nov-2007, 22:35
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Vector losing information [on reserve()?]


Quote:
Originally Posted by Algar
I think I may have found the problem. If the vector uses the copy constructor for some reason (strange) during reserve()
If the vector can't expand on the spot, what else could it do than to use the copy constructor?

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:
How can I copy an object when I don't know which child class it is ?
I don't know why you should know the class. Isn't it enough just to implement those methods? And if the class is dependent on runtime decisions, then how could you know the class? I don't think you need RTTI for this. I might be wrong.
__________________
Music, programming, endless learning..
  #5  
Old 05-Nov-2007, 11:25
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

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  
Old 05-Nov-2007, 17:41
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

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  
Old 05-Nov-2007, 23:34
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

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 GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 15:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.