Casting of bitstream types

Hello,
I have following system verilog code:

class myclass;
 
 shortint address;
 logic [3:0] code;
 byte command [2];
   function void foo(logic [3:0] code_param);
    code =  code_param;
  endfunction
 
endclass
 
typedef bit Bits [36:1];
 
module top();
myclass p;
Bits stream[$];
Bits b;
myclass q;
initial begin
  q = new();
 p.address = 4;
 p.code = 2;
  p.command[0] = 100;
  p.command[1] = 34;
 
 
 stream.push_back(Bits'(p)); // append packet to unpacked queue of Bits
 
 b = stream.pop_front(); // get packet (as Bits) from stream
 q = myclass'(b);
end
endmodule

This code gives error:

Error-[ICO] Illegal cast operation
testbench.sv, 32
Typecast fails because the use of class casting from incompatible type:
myclass’(b)
Only class object is supported.

2 errors
CPU time: .073 seconds to compile

If I replace myclass by a structure, this works and there is no error. LRM-2012 also states on page 100:

“An associative array type or class shall be illegal as a destination type. A class handle with local or protected members shall be illegal as a
source type except when the handle is the current instance this (see 8.11 and 8.18).”

Can anyone please explain why a class cannot be destination of casting of bitstream type. (class is allowed to be source but not destination)

Thanks,
-sunil puranik

In reply to puranik.sunil@tcs.com:

Because in the assignment
q = myclass’(b);
, q is expecting to be assigned with a handle to an object. There’s no mechanism defined for constructing an object.

I recommend that you do not use classes with bitstream casts, even as a source. Inheritance can mess up expected results,

Hi Dave,
thanks for the reply. i have another question not related to above but regarding your paper “Using parametrized classes and factories - Yin and Yang of object oriented verification”:
On page number 8, there is a factory class defined as below:

class Factory;
Object obj;
virtual function Object create();
// delegate creation to subtype itself
create = obj.create();
endfunction
endclass
Factory factory = new;

obj handle is set to different subtypes of object class in initial block and run() task calls create function of factory, which in turn calls create virtual function of subtype.
What i am not clear about is why should the create() function of factory be declared as virtual. Is it required to extend factory class later for some reason?
Thanks,
-sunil

In reply to puranik.sunil@tcs.com:
The general recommendation (from Java) is to make all public methods virtual, all protected and local methods non-virtual.

In the future, please ask a new question rather than replying to an old unrelated question. It makes searching more efficient.