Dynamic Binding Flashcards

1
Q

Why can’t a derived pointer reference a base object?

A

A base object does NOT have the memory for the derived members. The integrity of the data cannot be maintained.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax for downcasting with a constant reference?

A

const derived * ptr = dynamic_cast(& const_base_reference);
OR
const derived * ptr = dynamic_cast(& const_derived_obj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax for downcasting with a base pointer?

A

derived * ptr = dynamic_cast(base_class_ptr);

// Check to make sure it has memory
if(ptr)
      data_member = new derived(*ptr);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is downcasting?

A

Downcasting is the act of using a cast to type convert a base class pointer or const reference to a derived pointer at run time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where does upcasting take place? And what is the syntax?

A

In the argument list.

Syntax:
void function(base * pointer)
{
     p->other_function();    // Waits till run-time to perform operation and call the derived function because a base class IS a derived
}
main
    derived * ptr2 = &obj; // Creates a derived pointer to an object
     function (&obj);           // Passes in the object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is upcasting?

A

Upcasting is where you can have a base class pointer point to anything in the hierarchy. No type conversion takes place because a base IS its child. A base class pointer points to a derived or base class object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is RTTI important?

A

Constructors cannot be virtual and type conversion without a cast will result in a compilation error because the correct object is not being referenced at compile time. So, it is important to wait until runtime using the dynamic_cast operation to determine which object is being pointed to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly