We help IT Professionals succeed at work.

Managed version of std::vector?

bjqrn
bjqrn asked
on
Medium Priority
865 Views
Last Modified: 2013-12-03
I would like to use a std::vector in my managed class. The vector will hold a number of instances of another class I made, this class is managed too. How to do this?
Comment
Watch Question

For managed C++ you should probably use ArrayList

#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myList );
 
void main()  {
 
    // Creates and initializes a new ArrayList.
    ArrayList* myAL = new ArrayList();
    myAL->Add(S"Hello");
    myAL->Add(S"World");
    myAL->Add(S"!");
 
    // Displays the properties and values of the ArrayList.
    Console::WriteLine( "myAL" );
    Console::WriteLine( "\tCount:    {0}", __box(myAL->Count) );
    Console::WriteLine( "\tCapacity: {0}", __box(myAL->Capacity) );
    Console::Write( "\tValues:" );
    PrintValues( myAL );
}
 
void PrintValues( IEnumerable* myList ) {
    System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
    while ( myEnumerator->MoveNext() )
       Console::Write( "\t{0}", myEnumerator->Current );
    Console::WriteLine();
}

 /*
 This code produces the following output.
 
 myAL
     Count:    3
     Capacity: 16
     Values:    Hello    World    !
 */

Author

Commented:
I've tried with the ArrayList, but how do I call something in my class?

-----------------------------------------------------------------------------------------------
  System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
    while ( myEnumerator->MoveNext() ) {

        myEnumerator->Current->myFunction();
    }
-----------------------------------------------------------------------------------------------

Doesn't seem to work.

Commented:
Take a look at System.Collections.Generic Namespace.

Author

Commented:
But I use vs.net 2003, otherwise that seemed to be it.
Try changing

 while ( myEnumerator->MoveNext() ) {

        myEnumerator->Current->myFunction();
    }

to

 while ( myEnumerator->MoveNext() ) {

        MyClass *mc = (MyClass *)myEnumerator->Current;
        mc->myFunction();
    }

Author

Commented:
MyClass *mc = (MyClass *)myEnumerator->Current;

produces:

error C2440: '=' : cannot convert from 'System::Object __gc *' to 'MyClass __gc *'
Or if you prefer

while ( myEnumerator->MoveNext() ) {
       ((MyClass *)myEnumerator->Current)->myFunction();
    }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Sorry, I should have clarified..

MyClass, in the example I show refers to the name of the class that is being strored in the ArrayList.  Whatever the name of the class is that you are storing in the ArrayList should be substituted for MyClass...

Also... your class should be a managed class as well. To specify that it is a managed class include  __gc on the class definition...

it.

__gc class MyClass {
.
.
.

}

Author

Commented:
Yes, that's exacyly as it's done.

I rewrote the error message to make it more readable for anyone else than me.
>>error C2440: '=' : cannot convert from 'System::Object __gc *' to 'MyClass __gc *'
Hmmm...

I can't see why it won't allow "MyClass __gc *" to be converted to "System::Object __gc *".  Are you sure that your class is a managed class? I've only see this error when trying to convert a nonmanaged class to the manged class, in which case the error would have been:

error C2440: '=' : cannot convert from 'System::Object __gc *' to 'MyClass  *'

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.