Dynamic memory allocation with default values

Posted by viswanathan on Stack Overflow See other posts from Stack Overflow or by viswanathan
Published on 2010-06-10T17:17:02Z Indexed on 2010/06/10 17:52 UTC
Read the original article Hit count: 110

Filed under:
class A
{
private:
int m_nValue;
public:
A()
{
m_nValue = 0;
}
A(int nValue)
{
m_nValue = nValue);
~A()
{}
}

Now in main if i call

A a(2);// 2 will be assigned for m_nValue of object A.

Now how do we do this if i want to define an array of objects. Also how do we do this if i dynamically create objects using operator new like

A *pA;
pA = new A[5];// while creating the object i want the parameterised constructor to be 

//called

I hope the question is clear. Do let me know if more explanation is needed

© Stack Overflow or respective owner

Related posts about c++