How would I implement this?

Hello, I have been trying multiple things and thinking for a while, but I just can't think of the correct syntax/logic for it.

I have a base class and two derived classes. I am using dynamic binding to make a vector which stores instances of all 3 classes. Then, I am reading in from a file, it specifies which class it belongs to (I will use an if statement to check the string in the file, e.g. "base", "der1", "der2"). It will then push that onto the stack.

I can manage the above if there is only one of each class, however, there are multiples of each one. Therefore, something like the below code wont work:
vector<Base*> myVec;

1
2
3
4
5
6
7
8
9
Base *b = new Base;
Der1 *d1 = new Der1;
Der2 *d2 = new Der2;

//read the file and fill in the classes data members

myVec.push_back(b);
myVec.push_back(d1);
myVec.push_back(d2);


The above will just read each type of class once each and push them on. How would I implement something along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(int i = 0; i < lines; i++) //lines = how many lines in file
{
	cin.get(whatType, ':'); //reads a string up to the delim char :

	if(whatType == "Base")
	{
		//read line and fill rest of data members...
		myVec.push_back(b);
	}
	else if(whatType == "Der1")
	{
		//read line and fill rest of data members...
		myVec.push_back(d1);
	}
	if(whatType == "Der2")
	{
		//read line and fill rest of data members...
		myVec.push_back(d2);
	}
}

However, when the same class type is read in again, the previous one will be overwritten as well as it's a pointer to one instance? So outputting at the end will be incorrect. I want them all to be unique instances.

How would I go about doing that? I have no clue.
Last edited on
closed account (48T7M4Gy)
So if I understand you correctly, when you read in the data your problem is each group of data doesn't know which class it belongs to?

One simple way of solving this is to tag each object with its type by adding an extra field when it is saved and doing the reverse when it is read back from the file.

A tag could be something like a distinctive standout string such as "<Der1>" etc

That's more or less what you already have. Note that the rest of the group of data you read in each time is used to create the relevant object which in turn gets sentb to the appropriate vector.

You can tag each object or if you want to tag just a group then you will have to record on the file how many of that type follow :-)
Hi,

However, when the same class type is read in again, the previous one will be overwritten as well as it's a pointer to one instance? So outputting at the end will be incorrect. I want them all to be unique instances.

How would I go about doing that? I have no clue.


Are you sure - that approach should work fine: each object will have a different pointer. Even if the exact same object is pushed again it will still push a duplicate.

Cheers
It already knows which class it's using, I tested it with the constructor setting values and virtual functions to output the values.
The problem is being able to push back more than one 'Base'/'Der1'/'Der2' instance into the vector depending on the file. The file is never the same, apart from the format.

EDIT:
@TheIdeasMan,
I don't want duplicates though.

e.g. file =
6
base:1
der1:2
der2:3
base:4
der1:5
der2:6


6 means it has 6 lines to check, then it gets the type before the :, then sets the data to an int. The program would make the first 3 pushbacks' values change to 4, 5 and 6 respectively?
Last edited on
closed account (48T7M4Gy)
So what does it do if you pushback the objects by hardcoding rather than reading from a file?
I don't want duplicates though.


It doesn't matter.

The program would make the first 3 pushbacks' values change to 4, 5 and 6 respectively?


No. pushback pushes the object to the end of the vector, regardless of what the object is. So in your example, the vector would contain 1,2,3,4,5,6.

I guess the point of confusion is that you need to create a new object for each line read in the file.

closed account (48T7M4Gy)
That's more or less what you already have. Note that the rest of the group of data you read in each time is used to create the relevant object which in turn gets sent to the appropriate vector.


Funny that eh? If you aren't re-creating the object on retrieving it from the file then my guess is you are wasting your time. :-)
closed account (48T7M4Gy)
BTW if you don't want duplicates then maybe use a <map>?
Thank you for the replies.
I understand it now.

I've also included the declaractions of new pointers within the type checking statements.

It works nicely now.
closed account (48T7M4Gy)
:-)
Topic archived. No new replies allowed.