Regarding Dynamic casting

  1. Why The assignment from child_handle to parent_handle is always allowed?
    & " An assignment the reverse direction can only be checked at runtime using $cast " means what is going to be checked??

  2. My Second Question is even after using $cast i am not able to access child clas methods using parent class handle then what is the use of $cast???
    As an example :

class A;
   function dis;
    $display("   #### A Class #### ");
  endfunction
endclass

class B extends A;
  function dis1;
    $display("   ##### B Class #### ");
  endfunction
  endclass


program main;
  initial begin
  A a = new();
  B b = new();
  
    $cast(b,a);
  a.dis1();
  end
endprogram

This will give me error that " Could not find member ‘dis1’ in class ‘A’ " then what is the use of $cast?? & what is the solution if i want to do so.

Thanks
-Parth

Hi Parth,

Regarding 1)

We can assign child class handle to parent class handle because, all the properties of a parent class are visible to a child class. This is needed to access the child class methods using parent class handle… this is what we say polymorphism.
We can not assign the parent class handle to a child class handle as there are properties which only exists in a child class, which are missing in a parent class.

Regarding 2)

You don’t need to use downcasting (assigning a parent class handle to base class using $cast) to access the methods of child class using parent class handle. Just use polymorphism here.
I have modified your code. Please find it in below link.
polymorphism - EDA Playground

In reply to bdreku:

  1. But the example you have provide in polymorphism - EDA Playground has same method in both child as well as parent class. What if i want to access child class method which is not in parent class (using parent class handle to child method)

  2. And second one if we have created object of child class it is automatically call parent class constructor & access all methods of parent class means creating memory of child class it will create memory of parent class also. then & then child class can access parent class method (If I am wrong then correct me ).
    And if i am right then when we assign parent_handle = child_handle, all methods whether it is in child class or parent class should access by any of handle (parent_handle or child_handle ) because now we are assigning child_handle to parent_handle.

In reply to parth099:

Hi Parth,

As per my understanding,

  1. There is no any way to access child class methods using parent class handle without declaring those methods virtually in a parent class.

  2. By creating an object of a child class, does not create a memory for a parent class. But it creates a memory for child class based on the members declared in child class as well as in parent class. When you create an object for a parent class then it will create a memory for parent class based on the members declared in a parent class.
    E.g.,

class parent;
   int a;
endclass

class child extends parent;
   int b;
endclass

In above example, when you will create an object of parent class, it will create a 32bit (int a) memory for parent class, but by creating an object of a child class, it will create a 64bit (int a + int b) for child class. That does not mean that the child class object creates a memory for parent class.

@All, please correct me if I am wrong.

Regards,
H.Modh

In reply to bdreku:

Thanks ,

You said that"by creating an object of a child class, it will create a 64bit (int a + int b) for child class"
means when i create child object we have now memory for (int a + int b) , & then assign child_handle to parent_handle then why i can’t access child member through parent_handle??
I think we have memory of both variable a & b then what is the issue to get b signal through parent_handle b’coz parent_handle is a type of child_handle(parent_handle = child_handle )

In reply to parth099:

Parth099,

First of all, I prefer not to use the terms “parent” and “child” when talking about inheritance. Those terms imply there are two separate objects when you extend a class.

When you extend a class type from a base class type , the extended class type contains properties from both classes. When you construct an object of an extended class type, you get a handle to a single object that contains properties from both types.

When you declare a class variable of a particular class type, the compiler knows which properties are contained in that type, but is doesn’t know which object handle will be placed in that variable. It only knows that the type of that object will be the same class type of the variable, or some extension of that class type. This means the only guarantee that can be made is that object must contain class properties of a class constructed of the same type. It cannot allow references to class properties that may not be present. It’s not going to analyze the procedural flow of the code to see what could be possible. You can only access class properties from a variable based on the type of that variable, not the type of the object handle contained in that variable

In reply to dave_59:

Hi Dave,

Could you please throw a light on my reply, given above, regarding memory allocation during object creation. I just want to know that my understanding regarding memory allocation is correct or not. For your quick ref, I have quated my reply below.

  1. By creating an object of a child class, does not create a memory for a parent class. But it creates a memory for child class based on the members declared in child class as well as in parent class. When you create an object for a parent class then it will create a memory for parent class based on the members declared in a parent class.
    E.g.,
class parent;
int a;
endclass
class child extends parent;
int b;
endclass

In above example, when you will create an object of parent class, it will create a 32bit (int a) memory for parent class, but by creating an object of a child class, it will create a 64bit (int a + int b) for child class. That does not mean that the child class object creates a memory for parent class.

Thanks.
-H.Modh

In reply to bdreku:

Thanks Dave,
Now my all fundamentals regarding this are clear.

In reply to bdreku:

My wording would simply be…
By creating an object, you allocate memory based on the properties of the class. In the case of an extended class, it just so happens to include the properties of the base class.

In reply to dave_59:

Hi Dave,

I have been always receiving useful information from you always. On the same topic which Parth asked for, I have tried this in c++ and it’s allowing child class non virtual method from parent class handle using dynamic_cast. It requires only 1 method declaration as virtual in base class to make it polymorphic class.

Here is the code:
include iostream
using namespace std;

class Base {
public:
void show()
{
cout << " In Base \n";
}
};

class Derived : public Base {
public:
void display()
{
cout << “In Derived \n”;
}
};

int main(void)
{
Base* bp = new Derived;

// RUN-TIME POLYMORPHISM 
((Derived *)bp)->display(); 

return 0; 

}

I’m looking similar with system verilog class. I tried using $cast but it’s not allowing to access child class non virtual method.

Is it a limitation of SV over C++ by means of dynamic_cast?

Anyone’s view/comments are welcomed.

In reply to Geet:

What you show is not a dynamic_cast, it is downcasting with a static cast and quite a dangerous operation. If bp happened to hold a handle to something other than a Derived class, you would be jumping into garbage space. The way to safely do this in C++ is

int main(void) 
{ 
  Base* bp = new Derived; 
  Derived *d;
   //...
  d = dynamic_cast<Derived*>(bp);
   if (d) // make sure d is non-null
      d->display(); 

return 0; 
}

this is very similar to the way $cast works.