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.

Overridden members system verilog classes

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
I have below query.
Code:
class my_a;
int member1 = 1;
endclass
class my_ea extends my_a;
int member1 = 2;
endclass

Now when I do
Code:
my_a A;
my_ea EA;
EA =new();
A=EA;
================================================== ==========
EA = new(); has given handle to object of type my_ea to class variable EA.
A=EA; This pass the same handle (pointer value which points to object of my_ea) to A. so , A.member1 should refer to value 2.

But it refer to value 1. why?
 
Last edited by a moderator:

I'm not totally sure, but I presume that you should declare as local the int variable within a class that you want not it as publicly visible.
 

That is because A is of type my_a. You have no visibility of the sub-class without doing a $cast.

- - - Updated - - -

I'm not totally sure, but I presume that you should declare as local the int variable within a class that you want not it as publicly visible.

Declaring something local does do what you say, but it isnt to do wiht the OPs question.
The OPs is a question of scope.
Declaring the variable as local will just cause an error. It wont return the value of 2.

- - - Updated - - -

The easiest way around this would be to have a virtual function that returns the member1 variable:

Code:
class my_a;
  int member1 = 1;
  
  virtual function int get_member1();
    return this.member1;
  endfunction get_member1;
endclass

class my_ea extends my_a;
  int member1 = 2;

  virtual function int get_member1();
    return this.member1;
  endfunction get_member1;
endclass


my_a A;
my_ea EA;
EA =new();
A=EA;

A.get_member1();  //returns 2
 

I understood.

I understand that the way you described, I can get value 2 for member1.

But my question is different.

EA = new(); has given handle to object of type my_ea to class variable EA.
A=EA; This pass the same handle (pointer value which points to object of my_ea) to A. so , A.member1 should refer to value 2.

Can you explain me in this context?
 

This is because you declared A as class my_a, therefore you only have visibility of variables/functions declared in the my_a class. If you want to see member1 from the my_ea class you will need to cast A to the my_ea class.

This is important because you can have many sub-classes of my_a, with an array of variables all of different classes. The compiler can only see the class of the variable type, it doesnt know what class it is from the handle.

This can get a little confusing with virtual/non virtual functions. non-virtual functions will always call the function from the class type of the variable. virtual functions will always call the function from the class declared by the handle, because they get overridden in the base classes.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top