about dynamic_cast

When I delete the body content of Class B, errors occur on compilation.
I tried to replace "virtual void dummy() {}" with " int abc;" and the errors stills popped out on compilation.

As a beginner, I do not know why. Anyone can explain it?

Another question.
I see a pointer pointing to a base class can also be used to point to the derived classes but the members in the derived ones that are not inherited from the base class cannot be dereferenced by the pointer with an arrow operator.

Does that mean the pointer doesn't point to a complete object of the derived class type?



Appreciated.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// dynamic_cast
#include <iostream>
#include <exception>
using namespace std;

class Base { virtual void dummy() {} };
class Derived: public Base { int a; };

int main () {
  try {
    Base * pba = new Derived;
    Base * pbb = new Base;
    Derived * pd;

    pd = dynamic_cast<Derived*>(pba);
    if (pd==0) cout << "Null pointer on first type-cast.\n";

    pd = dynamic_cast<Derived*>(pbb);
    if (pd==0) cout << "Null pointer on second type-cast.\n";

  } catch (exception& e) {cout << "Exception: " << e.what();}
  return 0;
}
> When I delete the body content of Class B, errors occur on compilation.

To use dynamic_cast the pointer or reference must be one to a polymorphic object (ie. an object of a class type with one or more virtual functions).

To delete objects polymorphically, the base class destructor should be virtual.


> Does that mean the pointer doesn't point to a complete object of the derived class type?

There is a complete object; but the derived class members are not visible when it is accessed via a pointer or reference to the base class.
@JLBorges thanks so much,dude.

>To use dynamic_cast the pointer or reference must be one to a polymorphic object (ie. an object of a class type with one or more virtual functions).

Class Base is a polymorphic one, having a virtual member function.

Class Derived is derived from Base, so Derived also has that virtual member function, which means Derived is a polymorphic one, too.
So pba pointing to some Derived object can be an argument of dynamic_cast.

Am I right?
Yes.

The other question:
When I delete the body content of Class B, errors occur on compilation.

You did not show any code or actual error messages for that. How can we tell what syntax error you have?
@JLBorges

I have found the answer in the tutorial. Thanks.
"But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived)

polymorphic classes (those with virtual members)


if -and only if- the pointed object is a valid complete object of the target type. For example:"
> I see a pointer pointing to a base class can also be used to point to the
> derived classes but the members in the derived ones that are not inherited
> from the base class cannot be dereferenced by the pointer with an arrow
> operator.
suppose that you have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Base{
public:
	virtual void foo();
};

class Derived1: public Base{
public:
	void bar();
};

class Derived2: public Base{
public:
	void asdf();
};

int main(){
	Base *p;
	int n;
	std::cin >> n;
	if (n > 42)
		p = new Derived1();
	else
		p = new Derived2();

	p->foo(); //fine
	p->bar(); //¿what do you want to happen here?
	p->qwerty(); //¿and here?
}
Last edited on
Topic archived. No new replies allowed.