Adding objects to array in vc++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U3RlZmFuIFNvbGplbW8=?=

    Adding objects to array in vc++

    I trying to add objects to an array without success. The array is declared as
    A** arr**. It is allocated with with the new operator. Two instances of the
    class A is allready instanciated with the new operator and assigned to the
    array with the brackets [] as shown below. The problem is that both elements
    in the array is assigned the first obejct Test1.
    Note that the main class is managed.
    Im using Microsoft Visual Studio 2008.

    Thanks on advance!
    Stefan

    namespace Test {


    class A
    {
    public:

    virtual int Funca()
    {
    int a = 5;
    return a;
    }
    };

    public ref class Class2
    {
    public:
    void Test( void)
    {
    A *Test1;
    A *Test2;

    Test1 = new A();
    Test2 = new A();

    A **arr;

    arr = new A *[2];
    arr[ 0] = Test1;
    arr[ 1] = Test2;
    }
    };
    }

    Watch 1 window at the end
    + arr[0] 0x055E87F8 Test::A*
    + arr[1] 0x055E87F8 Test::A*

  • Mark Salsbery [MVP]

    #2
    Re: Adding objects to array in vc++

    "Stefan Soljemo" <ssstesoe@newsg roup.nospamwrot e in message
    news:80F2162D-078D-4C68-A78E-D9B119A41D57@mi crosoft.com...
    I trying to add objects to an array without success. The array is declared
    as
    A** arr**. It is allocated with with the new operator. Two instances of
    the
    class A is allready instanciated with the new operator and assigned to the
    array with the brackets [] as shown below. The problem is that both
    elements
    in the array is assigned the first obejct Test1.
    Note that the main class is managed.
    Im using Microsoft Visual Studio 2008.


    It's a bug.

    If you look at the array in a memory window, you'll see the two different
    pointers, so at least the compiled code works correctly :).

    I'll pass this on to Microsoft.

    Mark

    --
    Mark Salsbery
    Microsoft MVP - Visual C++



    >
    Thanks on advance!
    Stefan
    >

    Comment

    Working...