Last week i decided to write an application, quite large to use most of c++ facilities that av learnt so far just for fun, however i faced a bunch of problems that really choked my creativity in the first phase of my app but than i got some help , solved the main problems and ignored the rest coz i could still do without them but then there is this quite interesting one that got my mind totally twisted up n if possible i would like to know how i would have solved it ,might be i'll need the solution somewhere else i never know.
Description:
The problem was about tracking the number of copies generated per each class
instance, it seemed quite easy but i was wrong.
for example , i might have a class Foo then create two objects named Foo_obj1
and Foo_obj2, for my case i had a function called generate_n_copies that took an object,a count and returned a vector of count objects created using copy assignment operator only, also each copy created was assigned a numeric number to denote it's copy number.
so if i called
1 2 3 4 5 6 7 8 9 10
|
auto vec=generate_n_copies(Foo_obj1,10);
///the last item should have copy number as 11.
/// if i also called the function again like this
auto vec2=generate_n_copies(vec[7],10);///problematic exception
///i intended to have last item assigned 21 as it's copy number
///because they are all copies of Foo_obj1
/// Note : Foo_obj1 copy number==1;
|
I had 3 possible options to track my copies.
1. using a static variable - failed because i din't realize the variable would
belong to the class- the logic error was that all
instances of the class would use the same variable.
i.e Foo_obj1 and Foo_obj2 used the same variable.
2. using a stack/heap variables - could track the copies but failed on the
part labeled exception.
3. hybrid -> heap & static variable - failed as the first one.
The code for all the three instances is still available.
Conclusion: Totally impossible
If anyone got a nice idea on how to take this beast down i would really
appreciate.