Jump to content
Sign in to follow this  
qinhailiang

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×