Re: assert
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Tue, 30 Oct 2007 08:51:07 -0500
"Peter Oliphant" <poliphant@xxxxxxxxxxxxxxxx> wrote in message
news:O9dQSZxFIHA.5752@xxxxxxxxxxxxxxxxxxxxxxx
Sure!
value class valClass
{
int X ;
// NEEDS a constructor
valClass( int x) { X= x ; }
} ;
value classes aren't required to have a non-default constructor (and the
default constructor is system-defined to zero-fill and cannot be changed).
However since your member variable X is private, there'd be no other way to
change it which might give you compiler warnings.
typdef array<valClass> ValClassArray ;
ref class refClass
{
int X ;
// DOESN'T need a constructor, but helpful
}
typedef array<refClass^> RefClassArray ;
main()
{
// valClass
valClass vc ;
vc.X = 1 ;
can't work, X is private
valClassArray^ vc_array = gcnew valClassArray(3) ;
for(int i=0; i < 3; i++)
{
vc_array[i].X=i ;
}
//same as:
//vc_array[0].X = 0;
//vc_array[1].X = 1 ;
//vc_array[2].X = 2;
//ref Class
refClass^ rc = gcnew refClass(1) ; //rc->X=1 ;
refClassArray rf_array^ = gcnew refClassArray(3) ;
for(int i = 0; i < 3; i++)
{
rf_array[i] = gcnew refClass(i) ;
}
//same as:
//vc_array[0] = gcnew refClass(0) ;
//vc_array[1] = gcnew refClass(1) ;
//vc_array[2] = gcnew refClass(2) ;
} ;
If you study the above code some difference will start to be clear. For
example, one has to create (gcnew) EVERY single element of a ref class
array, while the value class immediately has the storage allocate when you
create the array POINTER. This shows some good advantages to value
classes.
void refMethod( refClass^ rc )
{
rc->X=1 // original rc changed
}
void valMethod( valClass vc )
{
vc.X=1 // internal vc COPY changed, original vc unchanged!
}
In refMethod, by passing JUST the pointer to rc it has virtually passed
all of the fields of rc (in this case just X). in valMethod by passing vc
it must copy every field of valClass separately and put it on the stack.
In this case it also just has X, but if valClass had a LOT of fields in it
ALL of them would need to be copied and placed on the stack. Thus for
classes with a large amount of required storage space for its variables,
the ref class appraoch is far more efficient since it need only pass a
single address.
Also, in the refMethod case, actions performed on rc are being performed
on the original rc passed in. Those performed on vc by valClass are
performed on a COPY of the orginal vc, and the original vc in unaffected.
There are advantage to both depending on your needs. For example, if you
want the method to update a variable, it should be a ref class variable.
If you just want to use the value of a variable for calculations, then a
value class variable is best. In a sense, a ref class variable has
read/write privileges while a value class variable only had read
privilages in terms of access (assuming the value class variable is not
passed in by pointer, in which case I beleive you can once again
manipulate the original). In affect, a pointer allows modification, the
instance itself, used as a parameter to a method, acts as if it is
read-only. And only value class variables can be passed in by instance (in
contrasr to pointer).
The BASIC language makes this VERY clear. There are two ways to pass a
variable to method in that language: byRef and ByVal. bY Ref is equivalent
to passing a variable via its pointer, allowing modification to the
original. By Val copies the variable and manipulates it leaving the
original alone.
Hope that helps a bit! : )
[==Peter==]
In valMethod
"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8BD5AFE2-2431-42DB-95F4-21AB061F61AC@xxxxxxxxxxxxxxxx
Thanks Peter,
Your reply is so comprehensive!
I have a question about your following comments,
Value classes have some major restrictions about what methods it can
have.
But their main usefulness comes in arrays. If you create an array of
reference class instances the array can only contain pointers to the
pointers of the instances, so each instance must be created
independently
(ala 'gcnew'). An array of value class instances need only have code
which
allocates memory for the array pointers itself, which in turn
automatically
allocates the memory for its elements.
Could you show some simple pseudo code please about the advantage of
value
class compared with ref class please? I am not 100% sure about your above
comments. But I am interested to learn from you.
regards,
George
"Peter Oliphant" wrote:
I have a further question, why do you add ref before class?
There are two basic kinds of classes (or structs, since structs are now
just
classes that are internally default public): reference classes and value
classes.
Reference classes must be created ala gcnew and saved in pointers. Value
classes, on the other hand, can be created as 'concrete' instances, much
like 'long' or 'int'. That is, one can just create an 'int' without a
pointer:
int i ;
So, if I define a class as a reference class I put the keyword 'ref' in
front of it. If it is a value class I put the keyword 'value' in front
of
it. Here is some sample code to make it clearer:
ref class refClass
{
// code
} ;
value class valClass
{
//code
} ;
refClass^ refClassPTR = gcnew refClass( ) ;
valClass valClassINSTANCE ;
The major advantage to a reference class is it can be passed into a
method
via just its pointer. It also frees up its own memory once nobody is
referencing it (garbage collection, hence the 'gc' in 'gcnew')..
Value class instances can be passed by pointer, but they can also be
passed
'by value'. That is, the method then is working with a new copy of the
instance, so the orginal stays unchanged. However, to do this, all of
the
members of the class are also passed and copied, which uses up more
resources (memory and time) than just passing by reference.
In contrat, any method which is passed a parameter via a pointer can
change
that parameter (the external value as well as the value internal to the
method).
If you are use to old-school structs, a valClass is more like one of
these
old struct's. That's because it can be thought as a 'concrete' entitity
holding the values of its components, in contrast to a pointer to memory
containing this data. A pointer can change what it is pointing to, a
struct
retains its values unless manually changed directly.
Value classes have some major restrictions about what methods it can
have.
But their main usefulness comes in arrays. If you create an array of
reference class instances the array can only contain pointers to the
pointers of the instances, so each instance must be created
independently
(ala 'gcnew'). An array of value class instances need only have code
which
allocates memory for the array pointers itself, which in turn
automatically
allocates the memory for its elements.
I typically use 'ref' classes more since they are more flexible, both in
terms of features and moveability (passing a pointer is typically faster
than passing all the data the pointer is pointing to). And now that we
have
great 'garbage collection', it is nice that an instance of a reference
class
will free up its own memory when no longer needed (i.e., if no longer
pointed to then no longer referenced and thus no longer needed). In
contrast, a value class instance often requires intentional deletion in
order to free up its resources (unless only used as a reference, in
which
case a reference class might have been a better choice for its
definition).
Hope that helps more than confuses! :)
[==Peter==]
"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F7F3302C-C41E-4764-840C-8287505F0E3D@xxxxxxxxxxxxxxxx
Thanks Peter,
The class is very useful. I have a further question, why do you add
ref
before class?
regards,
George
"Peter Oliphant" wrote:
I created and use this class, you might find it useful. It contains
some
useful methods, pretty easy to figure out from their names. Note that
I
use
'assert' to answer your question:
#include 'assert.h'
#define MY_DEBUG (true)
ref class My_Debug
{
public:
static void Assert( bool condition )
{
Assert( condition, "<no text>" ) ;
}
static void Show( String^ comment )
{
Console::WriteLine( comment ) ;
}
static void Assert( bool condition, String^ info )
{
if ( condition ) { return ; }
Console::WriteLine() ;
Console::WriteLine( info ) ;
Console::WriteLine() ;
assert(!MY_DEBUG) ; // when debug is on then assert false
}
static void Deny( bool condition )
{
Assert( !condition ) ;
}
static void Deny( bool condition, String^ info )
{
Assert( !condition, info ) ;
}
static void Abort( String^ info )
{
Assert( false, info ) ;
}
static void Abort()
{
Abort( "*abort*" ) ;
}
} ;
// [==Peter==]
"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@xxxxxxxxxxxxxxxx
Hello everyone,
I saw a couple of form of assert in code on Windows,
1. ASSERT;
2. assert;
3. _ASSERT;
4. _assert.
Which one is the most correct to use? I saw people always define
this
to
that, and I want to find the root one which is defined by Windows.
I also saw people manually define assert to NULL if macro DEBUG or
_DEBUG
is
not defined, is that necessary?
I feel assert is in a mess and I want to find a clear and unified
way
for
this item.
thanks in advance,
George
.
- References:
- Re: assert
- From: Peter Oliphant
- Re: assert
- From: George
- Re: assert
- From: Peter Oliphant
- Re: assert
- From: George
- Re: assert
- From: Peter Oliphant
- Re: assert
- Prev by Date: Re: Possible Bug: name of directory inhibits execution?
- Next by Date: Re: Possible Bug: name of directory inhibits execution?
- Previous by thread: Re: assert
- Next by thread: Re: assert
- Index(es):
Relevant Pages
|