Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 535749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:44:58+00:00 2026-05-13T09:44:58+00:00

This is going to be a really goofy question but is it possible to

  • 0

This is going to be a really goofy question but is it possible to do the following in C++/CLI?

// C++/CLI
public ref class Managed
{
    public:
        array<double>^ m_data;
        Managed(array<double>^% data) : m_data(data)
        {
        }

        void bar(int x)
        {
            System::Array::Resize(m_data, x);
        }
};
// C#
static void Main(string[] args)
{
    double [] d = new double[10];
    Foo.Managed f = new Foo.Managed(d);
    f.bar(5);
}

Such that, after calling f.bar(5) from Main, Main::d and f.m_data are the same “reallocated” array? I’ve tried with unmanaged references, pointers, pin_ptr and so forth but nothing. Any ideas? If this is just not possible, is there a reason why?

Using the reference tracking operator (%) I could do that if I would to resize in the constructor, that’s what it has kept me trying.

This is possible in C++ using references, unfortunately I don’t know how to cast a array^ to array^& nor I can use array^% as a member:

class Foo{
public:
    double *& m_data;
    Foo(double*& data): m_data(data)
    {
    }

    void bar(int x)
    {
        delete m_data;
        m_data = new double[x];
    }

    ~Foo(){
        delete [] m_data;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{

    double* data = new double[10];

    Foo f(data);
    std::cout << &data << "\n" << &f.m_data << "\n";
    f.bar(5);
    std::cout << &data << "\n" << &f.m_data;
    getchar();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:44:58+00:00Added an answer on May 13, 2026 at 9:44 am

    This is not possible, because references are stored by value: that is, the CLR does not support by-ref member fields. (Eric Lippert discusses why here (around the second list of bullet points).) So although d is passed by reference into Managed’s constructor, when Managed stores this in m_data, that takes a copy of Main’s reference to the size-10 array, and this copy is now independent of the original. The bar function passes f’s m_data reference — which is a now independent copy of Main’s reference — into Array::Resize, which modifies m_data to refer to a new size-5 array. But because m_data is an independent copy, that doesn’t affect Main.d — that still refers to the size-10 array.

    A possible alternative strategy is to encapsulate the array reference in a small shim class, and have Main create (and Foo.Managed accept) an instance of the shim class instead. Then bar could resize the array pointed to by the shim. Because Main.d and f.m_data would still be references to the same shim (and would thereby share a single reference to the real array), Main.d would see the resized array (through the shim).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is going to be a really dumb question, I just know it, but
This is going to seem like a really silly question but I can't figure
this is going to be a total newbie question but I can't really do
I suspect I am going to feel really stupid after posting this but here
I am sure this is something really simple, but I am going bonkers trying
This is going to be a stupid question but I have spent an inordinate
I've got a feeling this is going to be something really simple, but anyway.
Okay this is going to seem really dumb but bear with me please. A
This is going to sound really ghetto, but I need to print some Javascript
I thought this was going to be very simple but I am really out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.