How to delete an Object ?

Hi Dear,

I want to reinitialize (delete previous object and its data) on each iteration.
I am not using new operator to create class objects and also my destructor has nothing in its body. My project has a following structure.

-- "A.h"
class A
{
A();
~A();
.....
}

--- "B.h"

include"A.h";
class B::A
{
B();
~B();

void doSomthing();
.....
}

"myproject.cpp"

include "B.h"
void main()
{
...

for( ...)
{

B objectB;
B.doSomthing();

}

}


In my case this structure remains all the previous values in class A and class B. so, on next iteration it uses the previous values. But i think as i am recreating object so, it should create everything new.

Thanks in advance
It should be re-creating the B object in the for loop. At the end of the loop it is destroyed, and at the beginning a new one is made.
For loop is an example, but i use a function which called several time, each time i needs everything new.

Dear "L B" I am not understand your solution.
What are you doing in constructors? Are you initialize class members (by zero for example)? If not then class members will be recreated at the same place in stack and will be have the same values at each iteration. The same is true for local uninitialized variables in doSomthing().
Last edited on
Nothing in the constructors.

just like

MMPCbar::MMPCbar() { };

MMPCbar::~MMPCbar(){ };

I am also creating the object of the classes without new operator.

Then it is reason. As I already said members of you classes are creating at the same place in memory. You should initialize variables explicitly.
Then they're getting the memory from their previous instance. You need to initialize in the constructor.
Topic archived. No new replies allowed.