Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2003

Tip: Looking for answers? Try searching our database.

auto_ptr in managed code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill Burris - 20 Sep 2003 00:15 GMT
How do I use auto_ptr in managed C++?

Here is my existing code:

namespace Alta
{
  public __gc class CMDAQ
  {
     public:
        CMDAQ();
        ~CMDAQ();
     private:
        CDAQControl* itsDAQControl;
  };
}

namespace Alta
{
  CMDAQ::CMDAQ( )
  {
     itsDAQControl = new CDAQControl();
  }

  CMDAQ::~CMDAQ()
  {
     delete itsDAQControl;
  }
}

If I change this code to the following, I get "error C3633: cannot define
itsDAQControl as a member of managed Alta::CMDAQ"

namespace Alta
{
  public __gc class CMDAQ
  {
     public:
        CMDAQ();
        ~CMDAQ();
     private:
        std::auto_ptr< CDAQControl > itsDAQControl;
  };
}

namespace Alta
{
  CMDAQ::CMDAQ( )
  {
     std::auto_ptr< CDAQControl > dc( new CDAQControl() );
     itsDAQControl = dc;
  }

  CMDAQ::~CMDAQ()
  {
  }
}

thanks

Bill
Brandon Bray [MSFT] - 20 Sep 2003 04:20 GMT
> How do I use auto_ptr in managed C++?
>
[quoted text clipped - 5 lines]
>       private:
>          std::auto_ptr< CDAQControl > itsDAQControl;

Hi Bill,
Unfortunately, you cannot do this with the current C++ language. auto_ptr
is a native class, and it is not possible to embed that class in a __gc
class. I realize this is a burden, and believe me -- we are working to
improve this situation.

The next version of the Visual C++ will allow you to write a different
auto_ptr template that would achieve what you are trying to do.

Cheerio!

Signature

Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.

Bill Burris - 24 Sep 2003 20:34 GMT
>  auto_ptr
> is a native class, and it is not possible to embed that class in a __gc
> class. I realize this is a burden, and believe me -- we are working to
> improve this situation.

What other native classes are not usable in a _gc class?

I use managed C++ as the bridge between my native C++ code and C#.  If I was
doing pure .NET code, I would use C#.

Bill
Brandon Bray [MSFT] - 28 Sep 2003 04:28 GMT
> What other native classes are not usable in a _gc class?

With one exception, all native classes cannot be embedded in a __gc class.
The exception is POD types (a POD is short for "plain old data" and is a
class that neither has a user defined copy constructor nor a user defined
destructor, and contains only other PODs or scalar data).

> I use managed C++ as the bridge between my native C++ code and C#.  If I
> was doing pure .NET code, I would use C#.

I understand your position. This too is a sentiment we are looking to fix in
the next version of Visual C++.

Cheerio!

Signature

Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.

Bill Burris - 30 Sep 2003 16:44 GMT
> With one exception, all native classes cannot be embedded in a __gc class.
> The exception is POD types (a POD is short for "plain old data" and is a
> class that neither has a user defined copy constructor nor a user defined
> destructor, and contains only other PODs or scalar data).

I have been using destructors in my managed C++.  It seems to work ok.

Some __gc code which uses native classes:

CMDAQ::CMDAQ( IMessageHandler* pMessageHandler, int siteId, int
interfaceType )
{
  itsMessageDispatcher = new CMessageDispatcher();
  itsMessageDispatcher->itsMessageHandler = pMessageHandler;
  CDebug::Initialize( itsMessageDispatcher );
  try
  {
     itsDAQControl = new CDAQControl( itsMessageDispatcher, siteId,
interfaceType );
     itsHistogramGroup = itsDAQControl->GetHistogramGroup();
     itsPhantomHistogramGroup = itsDAQControl->GetPhantomHistogramGroup();
  }
  catch( char* str )
  {
     itsMessageDispatcher->DebugMessage( str );
     throw( new System::Exception( str ) );
  }
}

CMDAQ::~CMDAQ()
{
  delete itsDAQControl;
  delete itsMessageDispatcher;
}

This is used in my C# code as follows:

public class DAQ
{
  private static CMDAQ itsDaq = null;
  private IMessageHandler itsMessageHandler;

  public DAQ( IMessageHandler messageHandler, int interfaceType, int
siteId )
  {
     itsMessageHandler = messageHandler;
     itsDaq = new CMDAQ( itsMessageHandler, siteId, interfaceType );
  }

  public void Dispose()
  {
     itsDaq.__dtor();
  }

Dispose is called in the OnClosing funtion for my Form:

protected override void OnClosing( CancelEventArgs e )
{
  base.OnClosing( e );
  if( itsState == ProgState.DAQ )
  {
     itsDaq.Stop();
     itsDaq.Dispose();
     itsSettings.Save( @"c:\alta_prog\33\ProgramSettings.xml" );
  }
}

Bill
Bill Burris - 30 Sep 2003 17:03 GMT
> > I use managed C++ as the bridge between my native C++ code and C#.  If I
> > was doing pure .NET code, I would use C#.
>
> I understand your position. This too is a sentiment we are looking to fix in
> the next version of Visual C++.

Even better would be if I could do everything in C#.  For that to happen I
need serial port support in .NET, or device drivers for Motorola GPS
receivers which are usable in .NET.  Also Measurement Computing
http://www.measurementcomputing.com/, hardware needs to be accessible from
.NET, and a .NET version of Tetradyne DriverX
http://www.tetradyne.com/driverx.htm.

Support for USB would also come in handy.  I tried to roll my own but didn't
get very far.  What I did do is posted at
http://www.componentsnotebook.com/notebooks/csharp/deviceio.aspx.

Bill

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.