Is a class virtual because it is marked as such or because it contains at least one pure virtual method?
I was asked; "Is a class virtual because it is marked as such or because it contains at least one pure virtual method?" Hmmm... Good question. Let's find out.
module M;
class base_class;
virtual function void foo; endfunction
endclass
base_class p = new;
endmodule
https://www.edaplayground.com/x/32HJ
because the class base_class is not virtual. However, this does not:
module M;
virtual class virtual_base_class;
virtual function void foo; endfunction
endclass
virtual_base_class p = new;
endmodule
https://www.edaplayground.com/x/3RSk
because the class is marked as virtual, even though it contains no pure virtual functions. This will not compile, either, because the class is not marked as virtual, but does contain a pure virtual function:
module M;
class virtual_base_class;
pure virtual function void foo;
endclass
endmodule
https://www.edaplayground.com/x/4Fiv
A customer asked me about this. I didn't know the answer, so I wrote a few lines of code on EDA Playground. EDA Playground is great for that, because it's always on. You don't have to queue for licences, wait for EDA tools to start, create new files, fire up editors...
This code compiles fine:
module M;
class base_class;
virtual function void foo; endfunction
endclass
base_class p = new;
endmodule
https://www.edaplayground.com/x/32HJ
because the class base_class is not virtual. However, this does not:
module M;
virtual class virtual_base_class;
virtual function void foo; endfunction
endclass
virtual_base_class p = new;
endmodule
https://www.edaplayground.com/x/3RSk
because the class is marked as virtual, even though it contains no pure virtual functions. This will not compile, either, because the class is not marked as virtual, but does contain a pure virtual function:
module M;
class virtual_base_class;
pure virtual function void foo;
endclass
endmodule
https://www.edaplayground.com/x/4Fiv
So, a class is virtual because it is marked as such, even if it has no pure virtual methods. But, if a class does have a pure virtual method, it must be marked as virtual.
A customer asked me about this. I didn't know the answer, so I wrote a few lines of code on EDA Playground. EDA Playground is great for that, because it's always on. You don't have to queue for licences, wait for EDA tools to start, create new files, fire up editors...
Comments
Post a Comment