CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2001
    Location
    Bangalore, India
    Posts
    29

    Question Creating array of objects - No default constructor (needs initializer list)

    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] ??????????

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    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

  3. #3
    Join Date
    Oct 2001
    Location
    Bangalore, India
    Posts
    29
    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...?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    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
    Last edited by Paul McKenzie; September 26th, 2002 at 01:38 PM.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    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:
    Code:
    #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

  6. #6
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    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;
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  7. #7
    Join Date
    Oct 2001
    Location
    Bangalore, India
    Posts
    29
    Thanks Axter
    It works...

    Thanks paul mc. for your suggestions.

    Kannan

  8. #8
    Join Date
    Jul 2002
    Posts
    56

    could u explain this.

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

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

  9. #9
    Join Date
    Jul 2002
    Posts
    56
    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??
    Just Starting Dont Hit

  10. #10
    Join Date
    Oct 2001
    Location
    Bangalore, India
    Posts
    29
    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

  11. #11
    Join Date
    Jul 2002
    Posts
    56
    now how do i access the information in class apply???
    Just Starting Dont Hit

  12. #12
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    now how do i access the information in class apply???
    Code:
    ptrapply[i]->member = xxx;
    Succinct is verbose for terse

  13. #13
    Join Date
    Jul 2002
    Posts
    56

    and

    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
    Just Starting Dont Hit

  14. #14
    Join Date
    Oct 2001
    Location
    Bangalore, India
    Posts
    29
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured