473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Kamikaze Object

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.

Aug 15 '06 #1
5 1376
at***********@gmail.com wrote:
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.
Well, why not simply:

foo(2)(5);

??? foo(2) creates a temporary object that will be discarded as soon as
the expression evaluation is finished ...

Pierre
Aug 15 '06 #2
at***********@gmail.com wrote:
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.
Note that calling the destructor of an object does not relate in any way to
the memory consumption of that object. It just ends the lifetime. The
memory will afterwards be occupied by the dead body of the object until you
explicitly resue it. If you don't and the dead object goes out of scope,
the destructor is implicitly called a second time and you have undefined
behavior.
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;
At the return statement, there is a 4th invocation of the destructor.
}

This compiles, although i dont understand why it lets me call 3 times
the same destructor for the same
object.
You have undefined behavior. No diagnostics is required: that is why it
compiles.

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.
Why are you using a function object instead of a function in the first
place?

I would really appreciate any advice.
Just use a function. If you have a need for a function object, e.g., because
you want something stateful that collects data within a for_each() loop,
then a kamikaze object won't be useful anyway.
Best

Kai-Uwe Bux
Aug 15 '06 #3
<at***********@gmail.comschrieb im Newsbeitrag
news:11*********************@i3g2000cwc.googlegrou ps.com...
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.
Actually the destructor is called 4 times -- first with (*this).~foo() in
the constructor, then twice explictly in main and finally it is
automatically called when execution leaves the scope of 'function', that is
at the end of main.

Explicitly calling a destructor is rarely a good idea. If you implement your
own memory management for some container, it might be usefull, but in
every-day programming you should better forget about it.

If you want your function objects to be destroyed once they are no longer
used, you have two simple methods to do so:

1. Enclose the desired scope in braces, in your example

int main()
{
...
{ foo function(5); }
...
}

2. Use a temporary, anonymous object

int main()
{
...
foo(5);
...
}

Whatever you do, do not call the destructor explicitly, especially not in a
constructor.
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.
And how do you wrap the wrapper of the wrapper?
2.- declaring the object locally inside a loop that executes once. This
makes the code look strange.
Solution 1 above does the same without the loop.
3.- using a pointer to the object and then new and delete. But im still
left with a useless pointer.
Don't use pointers, unless there is good reason to do so.
I would really appreciate any advice.
Create a simple wrapper and let its user decide how long an instance of that
class should exist. If the users thinks, the wrapper should live beyond the
scope of a function, he can use new/delete; if he doesn't care how long it
keeps its data, he can define it at function level; and if he likes, he can
open a narrower scope or use a temporary object as described above.

HTH
Heinz

Aug 15 '06 #4
at***********@gmail.com wrote:
Hi
I've made a kamikaze function wrapper that executes the function(s)
when its constructor is called and ends calling its own destructor.
This is never a good idea. The destructor is called automatically when the
object is destoryed.
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.
Usually, they take up a very small portion of memory, if any at all. Also,
calling the destructor won't free their memory.
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; }
};
The size of an objects of this class is on most compilers 1 byte. After
optimization, it's quite possible that it's reduced to 0 bytes.
>
int main()
{
foo funcion( 5 );
funcion.~foo(); //Strange?
funcion.~foo();

return 0;
}
How about just:

int main()
{
foo(5);
}

You get a temporary that is constructed (which will call your operator()),
then immediately afterwards destoryed.
This compiles, although i dont understand why it lets me call 3 times
the same destructor for the same object.
The compiler doesn't care. And actually, the destructor is called again when
main() reaches its end. Technically, calling the destructor multiple times
results in undefined behavior, so don't ever do it.
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.
You don't need a loop to make a code block.
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 think you spend way too much time on thinking about how to remove an
overhead that is somewhere between negligible and non-existent.

Aug 15 '06 #5
Thanks for ilustrating me the basics of anonymous objects. I'll fetch
some more info on the net.

Aug 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. ...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.