A
atomik.fungus
Hi
I've made a kamikaze function wrapper that executes the function(s)
when its constructor is called and ends calling its own destructor. I
thought originally of this as a good idea to avoid some typing but
after watching things that I didnt thought would compile i am starting
to doubt.
The original idea for doing so was that once function-objects have done
their job they are a just a waste of memory space.
This is the creature:
#include <iostream>
using std::cout;
class foo {
public:
foo( int num ) { this->operator()( num ); (*this).~foo();
}
private:
void operator()( int &val ) { val += 1; cout << val; }
};
int main()
{
foo funcion( 5 );
funcion.~foo(); //Strange?
funcion.~foo();
return 0;
}
This compiles, although i dont understand why it lets me call 3 times
the same destructor for the same
object.
Other solutions that i come out with for destroying function objects
are:
1.- wrapping the wrapper in a function and declarate it locally inside.
My main goal of hiding the functions on which the "main" function
dependes is kept untouched.
2.- declaring the object locally inside a loop that executes once. This
makes the code look strange.
3.- using a pointer to the object and then new and delete. But im still
left with a useless pointer.
I would really appreciate any advice.
I've made a kamikaze function wrapper that executes the function(s)
when its constructor is called and ends calling its own destructor. I
thought originally of this as a good idea to avoid some typing but
after watching things that I didnt thought would compile i am starting
to doubt.
The original idea for doing so was that once function-objects have done
their job they are a just a waste of memory space.
This is the creature:
#include <iostream>
using std::cout;
class foo {
public:
foo( int num ) { this->operator()( num ); (*this).~foo();
}
private:
void operator()( int &val ) { val += 1; cout << val; }
};
int main()
{
foo funcion( 5 );
funcion.~foo(); //Strange?
funcion.~foo();
return 0;
}
This compiles, although i dont understand why it lets me call 3 times
the same destructor for the same
object.
Other solutions that i come out with for destroying function objects
are:
1.- wrapping the wrapper in a function and declarate it locally inside.
My main goal of hiding the functions on which the "main" function
dependes is kept untouched.
2.- declaring the object locally inside a loop that executes once. This
makes the code look strange.
3.- using a pointer to the object and then new and delete. But im still
left with a useless pointer.
I would really appreciate any advice.