gcnew does not generate an object instance?

P

pkolinko

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

//
---------------------------------------------------------------------------------
 
M

Marcus Heege

Hi

Re CODE #1: If you define a reference type variable without a hat, an object
will be created implicitly and the instance will be implicitly disposed when
the variable leaves scope. Like in C++ you have to use the dot (.) instead
of the -> for these kinds of variables.

Re CODE #2: You define two variables, a member variable in a reference class
and a local variable in the Form's constructor. Since the member variable is
never initialized, trying to invoke members on it will cause an exception.

Marcus
 
P

pkolinko

Thanks!

I understand #1 now.

Regarding #2, I was wondering if you could explain it a bit more:
The member variable p_usbapi is a private member of Form1,
and is initialized in the constructor.

I don't quite understand how a local variable is created.
I thought I was initializing the Form1 member variable p_usbapi in the
constructor.
 
P

pkolinko

Ok, I got it.
If the syntax is wrong, I actually get two different variables,
one local and the other member of class.

Thank you so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top