Jump to content

Why is different the declaration class handle before and after the super call?


Recommended Posts

Hi, all

 

   //Case One

class BasePacket;
    int A;
endclass : BasePacket


class My_Packet extends BasePacket;
    int C;
endclass : My_Packet


class BaseTest;

BasePacket PB[string];

virtual function void Create_PKT(string s);
    PB = new();
endfunction : Create_PKT

virtual function void Configure_PKT(string s);  
    PB.A = 1;
endfunction : Configure_PKT

virtual function void printP(string s);
    $display("BaseTest::PB[%s].A is %d", s, PB.A);
endfunction : printP

endclass : BaseTest


class My_Test extends BaseTest;

virtual function void Create_PKT(string s);
    My_Packet MP = new();
    PB = MP;
endfunction : Create_PKT

virtual function void Configure_PKT(string s);
//  My_Packet mp;
    super.Configure_PKT(s);
    My_Packet mp;  
    $cast(mp, PB);
    mp.C = 2;
    PB = mp;
endfunction : Configure_PKT

virtual function void printP(string s);
//  My_Packet mp;  
    super.printP(s);
    My_Packet mp;
    $cast(mp, PB);
    $display("My_test::PB[%s].C is %d", s, mp.C);
endfunction : printP

endclass : My_Test


BaseTest T1 = new();
My_Test T2 = new();

initial begin
      T1.Create_PKT("StringBase");
      T1.Configure_PKT("StringBase");
      T1.printP("StringBase");
      T2.Create_PKT("MY_String");
      T2.Configure_PKT("MY_String");
      T2.printP("MY_String");
end


//Output Information (Compiler Report Error)
//
//near "mp": syntax error, unexpected IDENTIFIER, expecting #





//Case Two

class BasePacket;
    int A;
endclass : BasePacket


class My_Packet extends BasePacket;
    int C;
endclass : My_Packet


class BaseTest;

BasePacket PB[string];

virtual function void Create_PKT(string s);
    PB = new();
endfunction : Create_PKT

virtual function void Configure_PKT(string s);  
    PB.A = 1;
endfunction : Configure_PKT

virtual function void printP(string s);
    $display("BaseTest::PB[%s].A is %d", s, PB.A);
endfunction : printP

endclass : BaseTest


class My_Test extends BaseTest;

virtual function void Create_PKT(string s);
    My_Packet MP = new();
    PB = MP;
endfunction : Create_PKT

virtual function void Configure_PKT(string s);
    My_Packet mp;
    super.Configure_PKT(s);
    //My_Packet mp;  
    $cast(mp, PB);
    mp.C = 2;
    PB = mp;
endfunction : Configure_PKT

virtual function void printP(string s);
    My_Packet mp;
    super.printP(s);
    //My_Packet mp;
    $cast(mp, PB);
    $display("My_test::PB[%s].C is %d", s, mp.C);
endfunction : printP

endclass : My_Test


BaseTest T1 = new();
My_Test T2 = new();

initial begin
      T1.Create_PKT("StringBase");
      T1.Configure_PKT("StringBase");
      T1.printP("StringBase");
      T2.Create_PKT("MY_String");
      T2.Configure_PKT("MY_String");
      T2.printP("MY_String");
end


//Output Information
//
//# BaseTest::PB[stringBase].A is           1
//# BaseTest::PB[MY_String].A is           1
//# My_test::PB[MY_String].C is           2

 

 

  I designed two cases as above, ran case one, the simulator reported error as above.

 

  Would like to tell me the different of the declaration class handle before and after the super call?

 

  Thank you in advanced.

 

BR

 

QIN

Link to comment
Share on other sites

That's just because declarations need to be first in procedural code.

 

Try this:

virtual function void Configure_PKT(string s);
//  My_Packet mp;
    super.Configure_PKT(s);
    begin
        My_Packet mp;  
        $cast(mp, PB[s]);
        mp.C = 2;
        PB[s] = mp;
    end
endfunction : Configure_PKT

virtual function void printP(string s);
//  My_Packet mp;  
    super.printP(s);
    begin
        My_Packet mp;
        $cast(mp, PB[s]);
        $display("My_test::PB[%s].C is %d", s, mp.C);
    end
endfunction : printP

(note the extra begin/end to create a new scope)

 

regards

Alan

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...