P
pasa_1
Do you see anything wrong with using constructor 3 followed by a
function foo() as I have defined below?
class A {
member_variable_1;
ptr_member_variable_2;
public:
// Constructor 1
A():
member_variable_1(initial value),
ptr_member_variable_2(NULL) { };
// constructor 2
A(m_var_1, m_ptr_val_2):
member_variable_1(m_var_1),
ptr_member_variable_2(m_ptr_val_2) { };
// constructor 3
A(m_var_1)
{
// do something with member_variable_1
// and don't bother about ptr_member_variable_2
// Since no memory was allocated for ptr_member_variable_2
// the d-ctor should not try to delete it?
// Question1. do you see anything wrong in this approach?
};
~A()
{
if (!ptr_member_variable) {
delete ptr_member_variable_2;
ptr_member_variable_2 = NULL;
}
};
// foo uses only member_variable_1,
// doesn't care about the ptr member variable
foo();
};
Thanks in advance.
function foo() as I have defined below?
class A {
member_variable_1;
ptr_member_variable_2;
public:
// Constructor 1
A():
member_variable_1(initial value),
ptr_member_variable_2(NULL) { };
// constructor 2
A(m_var_1, m_ptr_val_2):
member_variable_1(m_var_1),
ptr_member_variable_2(m_ptr_val_2) { };
// constructor 3
A(m_var_1)
{
// do something with member_variable_1
// and don't bother about ptr_member_variable_2
// Since no memory was allocated for ptr_member_variable_2
// the d-ctor should not try to delete it?
// Question1. do you see anything wrong in this approach?
};
~A()
{
if (!ptr_member_variable) {
delete ptr_member_variable_2;
ptr_member_variable_2 = NULL;
}
};
// foo uses only member_variable_1,
// doesn't care about the ptr member variable
foo();
};
Thanks in advance.