Can and how do I extend an array of objects during run time?

Hi, I created an array of objects, based on a class I created to hold data only, at the start of my program. During the run time, I may need to add an unknown number of additional objects to new data. Can I do this How can I increase an array of objects during run timge, one at a time as the need arises

For exmaple:

myClass aryObjects[10];

..............

How do I add to the aryObjects and make it a aryObjects[11]

Thanks.




Answer this question

Can and how do I extend an array of objects during run time?

  • blemmon

    This is a List Control(ListView) with unknow number of listing until run time when I load it up and it could be a few listing or a couple hundred of listing...

    Any way to dynamically add new object to anarray of objects(or sturct type variable)

    Thanks.



  • IceX

    This is how I declare initially,

    userData = new CUserContextData[numOfChecks];

    There is a listview(with checkboxes) control in my dialog window. When user checks additional listing item then I would need to create an additional userData object each time to hold the data coorsponding to that checked item. (Oh, I am yet to change from array to vector)

    My question is how do I declare this new item I mean, I can't use the same name over and over now can I



  • pven

    Hi, I've been working on other part of the project but this problem still exist for me. I was wondering, would vector store a class object that I defined which mainly to hold some data for me. I suspect it might be a sytax problem Becuase all the example I've seen on msdn using only the intergers like v.push_back(10);

    How do I pass an object to it
    userDataObjects.push_back(&newUserData);
    That is not working becuase compiler comes back complaining that it expects
    the arg to be of type :

    error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from
    'CUserContextData *' to 'const CUserContextData &'

    p.s., my class object has several members that are declare as wchar_t*. So, it's not just a simple varible or a constant number.

    Thank you.



  • tonyinbeijing

    Can you not use conditional statements to name objects that correspond to each checkbox That would solve that problem I think.

    i.e. if(checkbox1->selected==true)


  • Harendra Singh

    I'm getting Assertion error with the following line of code. Can anyone see what I'm doing wrong here Thanks.

    vector <CUserContextData> userDataObjects;

    vector<CUserContextData>::iterator userDataIter;

    userDataObjects.push_back(newUserData);



  • Joseph Kasende

    Many thanks to all that reply and gave help. I will now try that in my code. Thank you guys!

  • Craig Guyer - MSFT

    I don't quite understand your question, but this is how I'll answer it ;)

    If you can do it for an int, then you can do it for vector...


  • lchase4411

    One more question about this. Since this is unknow number of objects that I'm adding. How can I do the declaration for each new object It would be like <myClass> whatobjectname Can the object name be a string variable Thanks.

  • karls123

    Hi Thankk you for the reply. I open the #include<vector> and found

    void push_back(const _Ty& _Val)

    Do I change it to : void push_back(const _Ty* _Val)

    if not, then where and how do I change the template that you're referring to

    Also, below is my class definition, statement of instantiate an object of that class and a smaple of how I allocate memory for one of the member data. Do I need to create a Copy menthod for my class, otherwise, how does vector knows how to make a copy of my data

    Thank you.

    class CUserContextData

    {

    public:

    CUserContextData(void)

    {

    }

    virtual ~CUserContextData(void)

    {

    if(wcslen(shell) >0)

    delete [] shell;

    if(wcslen(homeDir) >0)

    delete [] homeDir;

    if(wcslen(primaryGroup) >0)

    delete [] primaryGroup;

    if(wcslen(symarkUserRef) >0)

    delete [] symarkUserRef;

    if(wcslen(LoginName) >0)

    delete [] LoginName;

    }

    public:

    bool newObject, bWinLogOn, bUnifiedID, symarkEnabled, UIDChanged;

    wchar_t *shell, *homeDir, *primaryGroup, *symarkUserRef, *LoginName, *contextName;

    LPCWSTR lpcwContextName;

    int symarkUID, listViewIndex, UID;

    };

    /////////////////////////////////////////////

    CUserContextData *newUserData = new CUserContextData;

    /////////////////////////////////////////////

    //Get the Shell

    hResult =ptSyUserObject->Get(CComBSTR("symark-Shell"), &var);

    if(FAILED(hResult))//New Sy User

    {

    DisplayError( hResult, L"Error obtaining symark-Shell. - RetrieveUserAtt");

    goto CleanUp;

    }

    temp = var.bstrVal;

    if(temp.length()>0)

    {

    lpcTemp = temp;

    newUserData->shell = new wchar_t[wcslen(lpcTemp)+1];

    wcscpy(newUserData->shell, (lpcTemp));

    }

    else

    newUserData->shell =NULL;

    VariantClear(&var);



  • mmmattias

    Here's an example for you... I like examples when I'm not sure what to do...

    //include this...

    #include <vector>

    std::vector<myClass> anyObjects; //That's it to declare it!

    std::vector<myClass>::iterator aOi; //this is what we're going to use to navigate through our vector

    //Adding objects to the vector. Push back adds the object to the end of the vector

    anyObjects.push_back(myObject1);

    anyObjects.push_back(myObject2);

    anyObjects.push_back(myObject3); //etc

    //A method of getting at the objects

    for(aOi = anyObjects.begin(); aOi!= anyObjects.end(); aOi++)

    {

    myTempObject = aOi;

    }

    //Another method of getting at the objects

    myTempObject = anyObjects.front(); //gets the first object in vector

    myTempObject = anyObjects.back(); //gets the last object in vector

    I hope this helps...

    Ljubica


  • Tim Greenwood

    Hey, the template parameter type is different with the type of the object you passed to push_back, one is reference type, one is pointer type

    remove the & in the push_back statement to pass reference.or you can change the template parameter of std::vector from CUserContextData& to CUserContextData*



  • grissett

    Have you thought of using a linked list instead. That is to have the object contain a pointer to the next object that gets created at run time.

    Thanks,
    Ayman Shoukry
    VC++ Team


  • Derek Lan

    As light walker says, you should really consider using one of the standard container types. I strongly suggest using std::vector. It offers syntax equivalent to builtin arrays, but can grow as needed. You can eighter add elements as you go (using push_back) or resize at once to the expected final size (using resize), growing and shrinking as many times as you want. You can pass a pointer to it's first element (&myvector[0]) to any function expecting a pointer to the first element of an array. You also don't have to worry about calling delete [] when you done with it (assuming you allocated it on the stack).

    Cheers,

    ::Camelo

  • vito1281

    you can try vector or list, they are nice to manage unknown number of objects.

  • Can and how do I extend an array of objects during run time?