locked
how to declare an array of objects for custom c++ class

    Question

  • Hi all

    I have declared a custom class header file in my project. I include the header file in my MainPage.Xaml.cpp class and can declare a single object for the class.

    But I am unable to create an array of objects for the same.

    Kindly help me with the solution.

    Thanks.

    Header file is :

    namespace

    metro


    {


    public

    refclassContactInfo



    {



    //array<Platform::String^>^ cimage;



    String^ cimage;



    String^ cname;



     


     


    public:



     


    ContactInfo()


    {


    cimage=

    " ";


    cname =

    " ";


    }



    ~ContactInfo()


    {


    cimage=

    " ";


    cname =

    " ";


    }



     


    voidAddr(Platform::String^ name, Platform::String^ image)



    {



    cimage = image;



    cname = name;



    }


    };



    }

    Friday, February 24, 2012 11:28 AM

Answers

  • Have you considered a Vector for this purpose? If not, Herb does a great overview of some collections about 31min into this talk.

    Writing modern C++ code: how C++ has evolved over the years
    TOOL-835T
    Speakers: Herb Sutter

    The Media Plugins Sample happens to demonstrate the syntax.

    void MainPage::scenario3StartVideo_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
    { 
        Scenario3VideoStabilization->RemoveAllEffects(); 
        Scenario4Video->AddVideoEffect("Windows.Media.VideoEffects.VideoStabilization", true, nullptr); 
        auto v = ref new Vector<String^>(); 
        v->Append(".mp4"); 
        v->Append(".wmv"); 
        v->Append(".avi"); 
        PickSingleFileAndSet(v, Scenario3Video);

    Does that help?

    -David


    Saturday, February 25, 2012 8:31 AM
    Moderator
  • In C++/CX with a ref class, you need to use the Platform::Array class, or something compatible with ref classes like the STL vector.

    Some sample code I've written:

    #include <assert.h>
    #include <algorithm> 
    
    // Create Array of 10 ints, and one of 2 Strings
    Platform::Array<int>^ arr1 = ref new Platform::Array<int>(10);
    Platform::Array<Platform::String^>^ arr2 = 
    ref new Platform::Array<Platform::String^>(2);
    
    // iterate arr1 using C++ array indexer notation []
    for(int i=0; i<arr1->Length; i++)
    {
    	assert(arr1[i]==0);
    	arr1[i]= i+1;
    }
     
    // iterate arr1 with STL for_each, squaring arr1 values
    std::for_each(arr1->begin(), arr1->end(), [](int &element) {
    	element = element * element;
    });
     
    // iterate arr1 values with C++ 'for each'
    int val;
    for each (int element in arr1) 
    { 
    	val = element;
    }
    
    // access arr2 values with C++ array indexer notation
    arr2[0] = "Zero";
    arr2[1] = "One";
    arr2[2] = "Two";   // Error: arr2 only has 2 elements 
    

    For your case:

    auto arr = ref new Platform::Array<ContactInfo^>(10);

    for(int i=0;i<10;i++)

        arr[i] = ref new ContactInfo();

    Platform::Array has overloads for the index operator so once you create the array you can use regular array syntax to access elements.

    -Steve

    Friday, March 02, 2012 7:02 PM
    Moderator

All replies

  • Have you considered a Vector for this purpose? If not, Herb does a great overview of some collections about 31min into this talk.

    Writing modern C++ code: how C++ has evolved over the years
    TOOL-835T
    Speakers: Herb Sutter

    The Media Plugins Sample happens to demonstrate the syntax.

    void MainPage::scenario3StartVideo_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
    { 
        Scenario3VideoStabilization->RemoveAllEffects(); 
        Scenario4Video->AddVideoEffect("Windows.Media.VideoEffects.VideoStabilization", true, nullptr); 
        auto v = ref new Vector<String^>(); 
        v->Append(".mp4"); 
        v->Append(".wmv"); 
        v->Append(".avi"); 
        PickSingleFileAndSet(v, Scenario3Video);

    Does that help?

    -David


    Saturday, February 25, 2012 8:31 AM
    Moderator
  • I am just a beginner in this regard.

    Is there no other way to declare a simple array of objects for a custom class?

    Wednesday, February 29, 2012 6:17 AM
  • In C++/CX with a ref class, you need to use the Platform::Array class, or something compatible with ref classes like the STL vector.

    Some sample code I've written:

    #include <assert.h>
    #include <algorithm> 
    
    // Create Array of 10 ints, and one of 2 Strings
    Platform::Array<int>^ arr1 = ref new Platform::Array<int>(10);
    Platform::Array<Platform::String^>^ arr2 = 
    ref new Platform::Array<Platform::String^>(2);
    
    // iterate arr1 using C++ array indexer notation []
    for(int i=0; i<arr1->Length; i++)
    {
    	assert(arr1[i]==0);
    	arr1[i]= i+1;
    }
     
    // iterate arr1 with STL for_each, squaring arr1 values
    std::for_each(arr1->begin(), arr1->end(), [](int &element) {
    	element = element * element;
    });
     
    // iterate arr1 values with C++ 'for each'
    int val;
    for each (int element in arr1) 
    { 
    	val = element;
    }
    
    // access arr2 values with C++ array indexer notation
    arr2[0] = "Zero";
    arr2[1] = "One";
    arr2[2] = "Two";   // Error: arr2 only has 2 elements 
    

    For your case:

    auto arr = ref new Platform::Array<ContactInfo^>(10);

    for(int i=0;i<10;i++)

        arr[i] = ref new ContactInfo();

    Platform::Array has overloads for the index operator so once you create the array you can use regular array syntax to access elements.

    -Steve

    Friday, March 02, 2012 7:02 PM
    Moderator