Click to See Complete Forum and Search --> : Creating array of objects - No default constructor (needs initializer list)


mailtokannan
September 26th, 2002, 12:10 PM
This is a question regarding creating array of objects using new.
This class doesnt have default constructor, i.e., should provide a initializer list.

Ex.
Class CTest
{
private:
int m_nIndex;
long m_lLongIndex;

public:
CTest(int nIndex, long lLongIndex); //No default constructor
}

CTest:: CTest(int nIndex, long lLongIndex): m_nIndex(nIndex), m_lLongIndex(lLongIndex)
{

}

void main(void)
{
CTest* ptrTest = new CTest[5] ??????????
// How to do this?
}


Thanks and Regards
Kannan S CTest* ptrTest = new CTest[5] ??????????

Paul McKenzie
September 26th, 2002, 12:28 PM
Your stuck, since operator new[] requires your class to have a default constructor.

Why not just create a default constructor, and set the appropriate member variables to reasonable defaults?

Regards,

Paul McKenzie

mailtokannan
September 26th, 2002, 12:33 PM
Yes u r right. Because of new operator...
I cannot have a default constructor, because of design issues.

One cannot create an object of the class i am creating without supplying the necessary parameter to construct and object.

Without those parameters the object is meaningless.

So i have to do this way.

Is it possible to do this?

Wild guess - using operator overloading or something like that...?

Paul McKenzie
September 26th, 2002, 01:35 PM
Without those parameters the object is meaningless.So if it's meaningless, don't allow usage of it until the parameters are set correctly, either they are set through construction (as they are now) or a secondary Init( ) function. If the user tries to call a member function on the object that is not properly initialized, return an error code, throw an exception, something to indicate that the object is in an invalid state.

As a live example, you can create an empty ifstream object, but it's useless unless the programmer calls the "open()" method, or constructs an ifstream with a file name parameter.

Regards,

Paul McKenzie

Paul McKenzie
September 26th, 2002, 01:49 PM
Another solution is to create a third constructor -- one that takes a structure of two integers. Then you can create a vector (which is much safer than "new") using the single argument constructor:

#include <vector>
#include <iostream>

struct IndexStruct
{
IndexStruct(int i1, int i2) : index(i1), longindex(i2) { }
int index;
int longindex;
};

class CTest {
public:
CTest(const IndexStruct& IS) : m_nindex(IS.index),
m_lLongIndex(IS.longindex) { }
//....
int m_nindex;
int m_lLongIndex;
};


//...
using namespace std;

int main()
{
// This creates 5 CTests, initialized with index = 1, and longindex = 2
std::vector<CTest> CT(5,IndexStruct(1,2));
//...
for ( int i = 0; i < CT.size(); i++)
cout << CT[i].m_index << " " << CT[i].m_lLongIndex << endl;
}

With vector, you eliminate the calls to new and delete, and you can initialize the vector with a single value. The value in the above case is a reference to the IndexStruct structure.

Regards,

Paul McKenzie

Axter
September 26th, 2002, 11:44 PM
Try first creating a pointer array, and then filling each item in the array with via appropriate constructor.
Example:

int main(int, char*)
{
const int Max = 5;
CTest** ptrTest = new CTest*[5];
for(int i = 0;i < Max;++i) ptrTest[i] = new CTest(i, i*3);
return 0;
}

mailtokannan
September 27th, 2002, 12:55 AM
Thanks Axter
It works...

Thanks paul mc. for your suggestions.

Kannan

potluck
September 27th, 2002, 02:30 AM
apply** ptrapply = new apply*[5];

for(int i = 0;i < Max ; ++i)
ptrapply[i] = new apply();

potluck
September 27th, 2002, 02:36 AM
How do you access the information in the class through the pointer??

With the other example how would i access ctest info from main
or better yet in mine
how do i access the apply class through the pointers i made for each obj of apply??

mailtokannan
September 27th, 2002, 02:48 AM
If i am not wrong...

---> apply** ptrapply = new apply*[5];

Creating array of pointers, each of type apply.
i.e., creting an array of size 5, each element is a pointer to apply object.

--->for(int i = 0;i < Max ; ++i)
--->ptrapply[i] = new apply();

Then allocating memory for each arry element with new.

Thanks
Kannan

potluck
September 27th, 2002, 02:53 AM
now how do i access the information in class apply???

cup
September 27th, 2002, 03:16 AM
now how do i access the information in class apply???


ptrapply[i]->member = xxx;

potluck
September 27th, 2002, 03:20 AM
ptrapply[i]->askname();

what do i make this equal to???

also if askname is in the base class and apply is derived from the base class how do i access the information

mailtokannan
September 27th, 2002, 04:19 AM
Hope this answers your questions...


#include <iostream>
using namespace std;

class CBaseTest
{
public:
int m_ba;
int m_bb;
CBaseTest(int a, int b) : m_ba(a), m_bb(b)
{
}

void ShowMessage(char* szMsg)
{
cout<<endl<<szMsg<<endl;
}
};

class CTest : public CBaseTest
{

public:
int m_a;
int m_b;

CTest(int a, int b);
~CTest() {}
};

CTest::CTest(int a, int b) : CBaseTest(a, b), m_a(a), m_b(b)
{
}

int main (void)
{
const int Max = 5;
CTest** ptrTest = new CTest*[Max];

for(int i = 0;i < Max;++i)
{
ptrTest[i] = new CTest(i, i*3);
cout<<endl<<ptrTest[i]->m_a<<"\t"
<<ptrTest[i]->m_b<<"\t"
<<ptrTest[i]->m_ba<<"\t"
<<ptrTest[i]->m_bb<<endl;
}
ptrTest[i]->ShowMessage("Will this be displayed???");

return 0;
}

Thanks and Regards
Kannan S