gcnew does not generate an object instance?



Hi everyone,

I am writing a small low level embedded USB application using C++/CLI
windows Forms.
I am sort of new to the C++/CLI and having trouble understanding what
happens in this very simple line of code:

I have a reference class pic_usbapi, declared in "pic_usbapi.h".
I want to create an object of the class and have a tracking handle for
it.
It should be simple to do, see CODE#1 and CODE #2

Basically, the problem is this:
--------------------------------------------
A) If I declare p_usbapi as an object without the hat, see CODE#1,
then no problem, but then p_usbapi does not seem to behave like a
tracking handle.
In particular, I can't access member functions using -> such as
p_usbapi->led1();
I thought addressing reference classes was always done through a
handle?

B) Okay, so what if I declare p_usbapi to be a handle, with the hat,
CODE#2
Then the code compiles, but i get an exception when trying to address/
assign anything that has to do with p_usbapi. The reason is that an
object apparently was not created and handle is still null. I thought
that "gcnew pic_usbapi" was supposed to create the object no matter
what.

What I really would like to understand is why in CODE#2, an object of
the type pic_usbapi is not created and the p_usbapi handle is NULL.

Thanks!


// ********************** CODE # 1 *********************************
// This code works:
//
---------------------------------------------------------------------------------
#include "pic_usbapic.h"
public:
Form1(void) //constructor for Form1;
{
InitializeComponent();
pic_usbapi^ p_usbapi = gcnew pic_usbapi; // Statement #1
}
//some code here
private:
pic_usbapi p_usbapi; //Declaration of an object, no hat.

// some time later a member function is called:
p_usbapi.led1(); // I don't want to do this, but it
works.
// p_usbapi -> led1(); // does not work here, because p_usbapi
is not a handle.
// weird, how can it not be a
handle, its a reference class!

//*********************************** CODE #2
**********************************************
//This code throws an exception during runtime, because p_usbapi object
does not exist:
//---------------------------------------------------------------------------------

#include "pic_usbapic.h"
public:
Form1(void) //constructor for Form1;
{
InitializeComponent();
pic_usbapi^ p_usbapi = gcnew pic_usbapi; // Statement #1
}
//some code here
private:
pic_usbapi^ p_usbapi; //Declaration of a tracking handle

// some time later a member function is called:
p_usbapi->led1(); // PROBLEM: Exception is thrown, as p_usbapi
object does not exist.
// I verified that p_usbapi does not
exist using the debugger.
// And, of course, also after compiling
the program

//
---------------------------------------------------------------------------------

.



Relevant Pages

  • gcnew does not generate an object instance?
    ... Then the code compiles, but i get an exception when trying to address/ ... // some time later a member function is called: ...
    (microsoft.public.dotnet.general)
  • Re: safer stl
    ... implementation's exception support. ... >> int main ... > You should try to catch by const reference, ... only with atmember function. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: C++ in ternms of C
    ... Access qualifier can be a part of a mangled member function ... you can visit private variable ... > the code compile over." ...
    (microsoft.public.vc.language)
  • Re: Verification of initialization of global objects???
    ... > A non-static member function must never be called without a completely ... > class MyClass ... > initialization. ... Just make it private. ...
    (comp.lang.cpp)
  • Re: safer stl
    ... > Exceptions are designed to catch errors as well as unexpected errors. ... > this simple rudimentary example of how to try, throw and catch an exception ... the '.at' member function, and not when accessing the vector ...
    (alt.comp.lang.learn.c-cpp)

Quantcast