Calling a constructor

I have an instantiated class, with the initialization code in the constructor.I want to be able to call the constructor again (to reset object data) without moving the reference to the object. Is there an easy way to do this? I don't want to have to put data initialization into a separate function.

[307 byte] By [fireball2000a] at [2007-10-2 22:07:43]
# 1
No way. Constructors are there to initialize a fresh object, not to reinitialize an existing one. What would be wrong with putting the init code into a separate (preferably private) function?
BIJ001a at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 2

// Java ain't no C++

void T::reinit() {

delete this;

*this = * ( new T());

}

BIJ001a at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 3
Construstors construct. They don't "reset."
_dnoyeBa at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 4

> > // Java ain't no C++

> void T::reinit() {

> delete this;

> *this = * ( new T());

> }

>

...and that ain't no C++ either ;-)

Sorry for the off-topic reply, but that code cannot stand uncorrected.delete this;

Only valid for heap-constructed objects, which you can't be sure of inside the function. Will crash otherwise.

If "this" was in fact on the heap, execution might indeed reach this line:

*this = * ( new T());

...only to crash there, since it's an illegal dereferencing of "this", which has just been deleted.

(Surviving the Access Violation, you'd then have a memory leak, since new T() will never be deleted.)

The correct C++ way of doing a re-initialization would make use of placement-new.

void T::reinit()

{

T::~T();

new(this) T();

}

McNeppa at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 5
What would be wrong with putting the init code into a separate (preferably private) function?Nothing - that's the way to go.
Maaartina at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 6

Exception in thread "main" java.lang.VerifyError: (class: Reconstructor, method: reconstruct signature: (Ljava/lang/Object;)V) Expecting to find unitialized object on stack

Friggin' class verifier won't let me call <init>() to reconstruct :-)

You can reinitialize an object if you disable the class verifier and write a bit of Java bytecode assembly language to call <init>(). You may want to set all instance variables to zero/null first. No, I'm not seriously suggesting anyone should do this.

sjasjaa at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 7
I am fairly certain that you can call the ctor again via JNI as well. (Not the generic 'you' because I would never do that.)
jschella at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 8
I was sure it was possible somehow in C++! Now we learned how, thanks for posting. Java still ain't no C++.
BIJ001a at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 9
> I was sure it was possible somehow in C++! > Now we learned how, thanks for posting. > > Java still ain't no C++.True, but can you knock Java for trying? :P
_dnoyeBa at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...
# 10
And to be really amusing one can create a java object in JNI without calling a ctor at all. Think of the possibilities for lazy initialization!
jschella at 2007-7-14 1:24:27 > top of Java-index,Core,Core APIs...