Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Variable of base class type accessing property of derived class? [System Verilog,UVM]

Status
Not open for further replies.

jdshah

Junior Member level 3
Joined
Dec 17, 2010
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Ahmedabad
Activity points
1,481
Here I have tried to mimic clone method of uvm_object.

Code:
typedef base_class;
typedef derived_class;
function base_class create();
  derived_class tmp;
  tmp = new();
  return tmp;
endfunction
class base_class;
  virtual function do_copy(base_class rhs);
  endfunction
  virtual function copy(base_class rhs);
	do_copy(rhs);
  endfunction
  virtual function base_class clone();
	base_class base;
    base = create();
	base.copy(this);
    return (base);
  endfunction
endclass
class derived_class extends base_class;
  int A = 5;
  function do_copy(base_class rhs);
	derived_class derived;
	$cast(derived,rhs);
	super.do_copy(rhs);
	A = derived.A;
  endfunction
  function display();
	$display("A is %0d",A);
  endfunction
endclass
 
module test();
  derived_class d1,d2;
  initial
  begin
	d1 = new();
	d1.A = 10;
    $cast(d2,d1.clone());
	d1.display();
	d2.display();
  end
endmodule

Output :
A is 10
A is 10
=========================================
Here at base.copy(this);
"base" variable is of type base_class, how come it access variable "A" which is defined in derived_class?
Correct me if I am wrong somewhere.
 

Because the do_copy function is declared virtual, it calls the version that is "lowest" in the inheritance order, which in this case is the do_copy function from the derived class.

This is quite important, for example, you had an array of base_class objects, which were all different child clases in reality (eg. derived_class1, derived_class2, derived_class3). If it didnt call the "lowest" version of do_copy you would have to manually cast each object to the correct class before calling do_copy. But the virtual ensures that the correct function is called.

The function must be declared virtual in the base class. if the function is overridden locally, it still remains virtual even if it is not explicitly typed (because it was declared virtual in the base class).
 

Because the do_copy function is declared virtual, it calls the version that is "lowest" in the inheritance order, which in this case is the do_copy function from the derived class.

This is quite important, for example, you had an array of base_class objects, which were all different child clases in reality (eg. derived_class1, derived_class2, derived_class3). If it didnt call the "lowest" version of do_copy you would have to manually cast each object to the correct class before calling do_copy. But the virtual ensures that the correct function is called.

The function must be declared virtual in the base class. if the function is overridden locally, it still remains virtual even if it is not explicitly typed (because it was declared virtual in the base class).

Thanks for reply.
My question is different.
in base.copy(this); I assign value to variable A for object base. But base object is of type base_class.
base_class do not have variable A.
 

the $cast function casts rhs to a derived_class object, and you call the copy function, which in turn calls do_copy of the derived class.
If the context of the code you called, this refers to derived_class because d1 is a derived_class object
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top