Reputation: 249
I'm new to C++. In the following method, I tried to print without returning. That's fine. What I need is I need to return as follows. (Need to read result with this while loop). I know once return we can't loop back. Problem is result should be returned in every iterations as soon as result is available. (similar to one thread read result once it is available while other thread is running this loop). Can I use callback to resolve it?
list<string>MyLogRetriever::handleEventsOnEventID(std::string logType, DWORD eventID)
{
while (GetAsyncKeyState(VK_ESCAPE) != true)
{
wstring wsLogType = stringToWidestring(logType);
LPCWSTR lpcwstrLogType = wsLogType.c_str();
string string_query = "";
string_query = string_query + "*[System/EventID= " + to_string(eventID) + "]";
wstring wsCons = stringToWidestring(string_query);
LPCWSTR pwsQuery = wsCons.c_str();
getEvents(lpcwstrLogType, pwsQuery, -1);
//printResultedEvent(myLogStructures, numberOfAvailableEvents);
return returnResultedEvent(myLogStructures, numberOfAvailableEvents);
numberOfAvailableEvents = 0;
}
}
Upvotes: 1
Views: 203
Reputation:
What you are asking for is similar to a generator:
In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. In fact, all generators are iterators. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values. However, instead of building an array containing all the values and returning them all at once, a generator yields the values one at a time, which requires less memory and allows the caller to get started processing the first few values immediately.
While this feature is widely known and supported in other language such as Python, there is no official support of generators in C++ yet.
As a workaround, you might want to use a table and pass it as an argument to your function and also append each result to it and return it each time instead - you could write a class with a method next()
to yield the next result (credits go to Siyuan Ren for this).
Upvotes: 3
Reputation: 1495
Functions mathematically are constructs that perform a calculation and return a result. Function generally refers to a binary relation, that has a unique mapping for every item in it's domain(set of inputs) to it's codomain(set of outputs).Now, those set elements could themselves be aggregate.
I am not an expert in other languages such as Java or Python, but I guess this holds true for them too.
As per your use case, you could return an aggregate(vector,map,set etc) from the function as output.
Upvotes: 1
Reputation: 7057
There is only one way to return from a method. If you want to capture more information, you need to append to some sort of structure (eg. an array, or other couplex object), and return that instead. In your case, you would declare it before your while loop is executed, and use that inside the loop. Or you can call the method multiple times with different parameters.
Upvotes: 2