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 8175793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:54:15+00:00 2026-06-06T22:54:15+00:00

In C++/CLI it is not possible to put pointers to native C++ classes in

  • 0

In C++/CLI it is not possible to put pointers to native C++ classes in managed .NET generic collections, e.g.

class A {
public:
    int x;
};

public ref class B {
public:
    B()
    {
        A* a = GetPointerFromSomewhere();
        a->x = 5;
        list.Add(a);
    }
private:
    List<A*> listOfA; // <-- compiler error (T must be value type or handle)
}

is not allowed. I could of course use std::vector<A*> list; but then I could only make list a member of a managed class by using a pointer and it feels unnatural to use pointers to STL containers.

What is a good way to store native C++ pointers in .NET generics? (I’m not interesting in resource management here; the object the pointer points to is managed elsewhere)

  • 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-06-06T22:54:19+00:00Added an answer on June 6, 2026 at 10:54 pm

    The method I’ve been using is to wrap the pointer in managed value class, and then overload the dereferencing operator:

    template<typename T>
    public value class Wrapper sealed
    {
    public:
        Wrapper(T* ptr) : m_ptr(ptr) {}
        static operator T*(Wrapper<T>% instance) { return instance.m_ptr; }
        static operator const T*(const Wrapper<T>% instance) { return instance.m_ptr; }
        static T& operator*(Wrapper<T>% instance) { return *(instance.m_ptr); }
        static const T& operator*(const Wrapper<T>% instance) { return *(instance.m_ptr); }
        static T* operator->(Wrapper<T>% instance) { return instance.m_ptr; }
        static const T* operator->(const Wrapper<T>% instance) { return instance.m_ptr; }
        T* m_ptr;
    };
    

    I can then use the pointer naturally as follows:

    public ref class B {
    public:
        B()
        {
            A* a = GetPointerFromSomewhere();
            a->x = 5;
            list.Add(Wrapper<A>(a));
            Console.WriteLine(list[0]->x.ToString());
        }
    private:
        List<Wrapper<A>> listOfA;
    }
    

    Any improvements welcomed…

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

Sidebar

Related Questions

I'm writing a mixed C++/CLI DLL with both managed and unmanaged classes. Both need
Possible Duplicates: Conditions when finally does not execute in a .net try..finally block In
Here's my situation: I have .NET wrapper-objects in a C++/CLI layer that hold pointers
When using Microsoft Visual C++ (not CLI, just standard native C++) does inline assembly
Is there any Operating System that do not support threading? I read somewhere CLI
I have a C++/CLI wrapper around native .lib and .h files. I use the
Using C++/CLI, How to display a managed control (eg. System::Windows::Forms::Panel^) on a window created
The .net CLI is standardized as both ECMA-335 and ISO/IEC 23271:2006 - is it
I have declared C++/CLI class as below namespace testcominterface { [ComVisible(true)] [Guid(FFCA805F-8DAB-4AF8-A7B7-B488136E8177)] public interface
I've written a simple GDI-based data plotter using C++/CLI but it's not particularly fast

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.