G
Grahamo
Hi,
I have a basic question regarding some legacy code I'm working with;
Basically the code looks something like this. I'd like to know if there
are any reasons why a particular approach is taken.
Given a type X we have a class something like this;
class foo
{
public:
foo (X* x)
{
m_x = new X(); /// my question is about these 2 lines.
*m_x = *x;
}
~foo()
{
delete m_x;
}
protected:
X* m_x;
};
Now in the foo ctor above, I would have personally done something like;
foo(X* x)
{
m_x = new X(*x);
}
thereby avoiding the overhead of the default ctor call and then the
assignment, we would just use the copy ctor.
I am asking this question in the context of memory leaks. There's
nothing wrong, in the non-purist sense of the word, with the original
code. I mean it's not as efficient as it could be but it doesn't leak
and it does add memory resource concerns to the class, does it?
thanks and have a nice day
Graham
thanks and have a nice day
G
I have a basic question regarding some legacy code I'm working with;
Basically the code looks something like this. I'd like to know if there
are any reasons why a particular approach is taken.
Given a type X we have a class something like this;
class foo
{
public:
foo (X* x)
{
m_x = new X(); /// my question is about these 2 lines.
*m_x = *x;
}
~foo()
{
delete m_x;
}
protected:
X* m_x;
};
Now in the foo ctor above, I would have personally done something like;
foo(X* x)
{
m_x = new X(*x);
}
thereby avoiding the overhead of the default ctor call and then the
assignment, we would just use the copy ctor.
I am asking this question in the context of memory leaks. There's
nothing wrong, in the non-purist sense of the word, with the original
code. I mean it's not as efficient as it could be but it doesn't leak
and it does add memory resource concerns to the class, does it?
thanks and have a nice day
Graham
thanks and have a nice day
G