How to clear object?

I have object called Sources, which is member of Singleton S.

 
std::vector<SRC> Sources;


When I parse arguments in one method, I declare temporal SRC object.
 
SRC src_temp;

and then I do loop, where I change the temporal instance of SRC type. Because every command line argument can add different option which affects the content of src_temp object. So on the end of the loop there is this
 
S::I().Sources.insert(S::I().Sources.begin(), src_temp);

to add the source to vector of sources. Now my question is, how to clear the src_temp object, to be empty or not containing any data which I put there. Because it would make problems when I would continue in the loop without clearing it. The values from previous arguments would be added to the new argument processed and that why I want to clear it.

Yet here is the SRC type and TARGET type, which would be used in similar way:
http://paste.ofcode.org/6WmNgHBNXCu8rFgHtXw3v5
Last edited on
S::I().Sources.insert(S::I().Sources.begin(), src_temp);
S::I().Destinations.insert(S::I().Destinations.begin(), dest_temp);
src_temp.clear(); dest_temp.clear();

CLParser.cpp(152): error C2039: 'clear' : is not a member of 'SRC'

src_temp is not vector. It is single instance of SRC.
So in the loop you're doing something to add to your src_temp object each time? If src_temp is declared outside of the loop, you'd have to handle your own way of cleaning out your object to "reset" it back to what you want for the next iteration, we don't know what your src class looks like and it shouldn't matter.

This might not be what you want, I'm not sure, but why not make the temp object inside the for loop so that it cleans itself each iteration?

1
2
3
4
5
6
7
8
std::vector<SRC> Sources;
	
for (int i = 0; i < 10; i++)
{
    SRC src_temp;
    //add stuff to temp
    //push_back or insert to Sources
}

Last edited on
Well, I didn't know that's OK to redeclare it in the loop. But that's the simplest way. Thanks.
Either provide a member function to clear the object or reduce the scope of the object during the loop
"reduce the scope of the object during the loop "
Im not sure what you mean

Maybe one more problem... In the singleton I create the instances:
1
2
std::vector<SRC> Sources; // Sources obsahuje Options
std::vector<TARGET> Destinations;

and when I first run the first circle of the loop ... in the moment when
 
S::I().Sources.insert(S::I().Sources.begin(), src_temp);

was processed, there is created new element so now I have two elements in the Sources vector instead one. Something is wrong here because I need that the first circle should update the first element. Can you gimme tip how to do it?
reduce the scope of the object during the loop

LowestOne means the same thing that Ganado showed: put the src_temp object inside the loop.

every command line argument can add different option which affects the content of src_temp object

Could you do something like this:
1
2
3
4
5
6
7
8
9
SRC partial;
// Copy command line options into partial
for (int i = 0; i < 10; i++)
{
    SRC src_temp(partial)
    //add other stuff to temp
    //push_back or insert to Source
}
I did this:
1
2
3
4
5
6
7
8
SRC Partial1;
TARGET Partial2;

for (std::vector<std::string>::iterator it = raw_args.begin(); it != raw_args.end(); ++it)
{
SRC src_temp(Partial1);
TARGET dest_temp(Partial2);
}

But it does not work. It is the same. Is it the "(1) empty container constructor (default constructor)
Constructs an empty container, with no elements."? They write that when the constructor is empty is sthould create no elements. So why I had and have the elements there. thats mystery for me. Id think that the vector must be initiated so it cannot be empty, but of sure if it could be empty, that would be the best.
Last edited on
It possible to create the vector so that id havent any elements?
std::vector<SRC> Sources;
What does the default constructor do? http://www.cplusplus.com/reference/vector/vector/vector/
It says "(1) empty container constructor (default constructor)
Constructs an empty container, with no elements." but I see there element 0 with filled values as it is initiated.
I see there

Where?
Ah, I have found I have error in the code:
1
2
3
4
5
6
7
8
9
10
11
for (std::vector<std::string>::iterator it = raw_args.begin(); it != raw_args.end(); ++it)
	{
	SRC src_temp(Partial1);
	TARGET dest_temp(Partial2);
		i++;
		arg=(*it);
		std::cout << ": raw: " + arg + "\n";
		// Let's suppose that there is -r instead source to define multiple source files
		if (i == 1)
			S::I().Sources.push_back(SRC());
	}

The last line inserts the element which I expected should be on the end. And I think I should use push_back instead of insert.

BTW: Do you know if is it possible to set hotkey for Watch window in Visual Studio 2010? Id like to set my own.
Topic archived. No new replies allowed.