RAII

N

Naren

Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.

Application created using Microsoft compiler.

Thanks in advance.

Thanks,
Naren.
 
G

Guest

Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.

In what way? I would probably use the first one, but it depends on what
A and B are and how I would use them.
 
N

Naren

Erik Wikström said:
In what way? I would probably use the first one, but it depends on what
A and B are and how I would use them.

Since I am in the destructor, any issues?

Thanks for the response.

Thanks,
Naren.
 
M

Markus Moll

Hi
Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.

No, but the first one will (probably... see further below). The second one
suffers from both a missing copy-constructor and a missing assignment
operator. Try (with the second one)

int main()
{
B b1;
B b2;
B b3 = b1;
b2 = b3;
}

This is very likely to crash. I also don't see why you call the
functions "Doif" and "Doelse", because I fail to see any "decision" here.
Is first one undefined, though I have verified that both works correctly.

Um... If you have verified that both work correctly, then why do you ask?
No, the first one should be fine and better than the second. However, of
course it also depends on A, so without knowing what A does, it's hard to
judge if even your first attempt is correct.
Application created using Microsoft compiler.

That shouldn't matter.

Markus
 
T

terminator

Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}

};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}

};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.

Application created using Microsoft compiler.

Thanks in advance.

Thanks,
Naren.

first one looks more efficient and likely to work,the second deals
with dynamic allocation which is far more complex to handle.

regards,
FM.
 
D

Daniel Kraft

Naren said:
Since I am in the destructor, any issues?

No, as in the second version, a is destructed after the destructor of B
is finished, so in it you can work with a to your liking.

Cheers,
Daniel
 
R

R Samuel Klatchko

first one looks more efficient and likely to work,the second deals
with dynamic allocation which is far more complex to handle.

Dynamic allocation is more complex to handle because it gives you the
ability to detect low memory conditions. When the heap is full (or too
fragmented) to fulfill your request, new will throw and you will
probably want to handle that situation.

But if the stack is full, when you try to put another object on the
stack, the behavior is undefined.

samuel
 
R

Rolf Magnus

R Samuel Klatchko wrote:

Dynamic allocation is more complex to handle because it gives you the
ability to detect low memory conditions. When the heap is full (or too
fragmented) to fulfill your request, new will throw and you will
probably want to handle that situation.

Depends. A typical desktop system will become unresponsive long before that.
 
T

terminator

Dynamic allocation is more complex to handle because it gives you the
ability to detect low memory conditions. When the heap is full (or too
fragmented) to fulfill your request, new will throw and you will
probably want to handle that situation.

But if the stack is full, when you try to put another object on the
stack, the behavior is undefined.

but you have to take the risk of ill-programming the
[de]allocation,and as mentioned above (by the experts) you will need
to define the assignment and copy-ctor in addition to default-ctor and
dtor.
on the other hand large objects should be kept of stack and one can
use some smart pointer(eg. boost::shared_ptr or std::auto_ptr) to an
object instead of encapsulating a pointer inside an object:

class B;//a huge data type
std::auto_ptr<B> bptr (new B);
bptr->DoSomthing();
DoSomthingWith(*bptr);


regards,
FM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top