Wrox Programmer Forums
|
BOOK: Professional C++, 2nd Edition
This is the forum to discuss the Wrox book Professional C++, 2nd Edition by Marc Gregoire, Nicholas A. Solter, Scott J. Kleper ; ISBN: 978-1-1181-6995-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++, 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 26th, 2016, 10:42 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 9, page 299, dynamic_cast

What is erroneous about the statement:
Code:
Derived& dr = dynamic_cast<Derived&>(br);
"br" is the base reference object that is being downcast to a derived reference object "dr". What is illegal?
 
Old April 26th, 2016, 01:38 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

It depends on what br really is.
For example, if br is really a Base reference to an instance of Derived, then the dynamic_cast will work. However, if br is really a Base reference to an instance of Base, then the dynamic_cast will throw an exception.
 
Old April 27th, 2016, 12:53 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 9, page 299, dynamic_cast

Marc,

Here is how I interpret what you are saying:
br as a base reference to an instance of Derived
Code:
class Base
{
public:
  Base() {};
  virtual ~Base() {}
};

class Derived : public Base
{
public:
  Derived() {}
  virtual ~Derived() {}
};

int main()
{
  Base base;
  Derived derived;

  Base& br = derived;

  try {
    Derived& dr = dynamic_cast<Derived&>(br);
  } catch (const bad_cast&) {
    cout << "Bad cast!\n";
  }

  return 0;
}
The dynamic_cast will work, and it does.

However, if br is really a Base reference to an instance of Base, then the dynamic_cast will throw an exception.
Code:
class Base
{
public:
  Base() {};
  virtual ~Base() {}
};

class Derived : public Base
{
public:
  Derived() {}
  virtual ~Derived() {}
};

int main()
{
  Base base;
  Derived derived;

  Base& br = base;

  try {
    Derived& dr = dynamic_cast<Derived&>(br);
  } catch (const bad_cast&) {
    cout << "Bad cast!\n";
  }

  return 0;
}
Yes, the above throws an exception. But, what is the statement doing to make it throw an exception? I don't see the illegality. I see a downcast.
 
Old April 30th, 2016, 04:26 AM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

br is referring to a base instance, so *it is* a base instance, it is *not* a derived instance, so you cannot cast it to derived.
You can only downcast if the instance you have *really is an* instance of the class to which you downcast.
 
Old April 30th, 2016, 09:34 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 9, page 299, dynamic_cast

Marc,

Thank you very much. Your answer explains why the Base reference to an instance of Derived works and why a Base reference to an instance of Base, will throw an exception.

Again, thank you very much.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 8 Ajax.ActionLink, at page 197. PartialView displayed as its own page. JimiFriis BOOK: Professional ASP.NET MVC 4 1 March 25th, 2015 04:18 AM
Chapter 8 - Page 232, Step 5 Building the Master Page bisigreat BOOK: Professional SharePoint 2010 Branding and User Interface Design 0 May 8th, 2013 12:58 PM
chapter 2 - page 41 problem creating the page.. jorjiana BOOK: Beginning Oracle Application Express ISBN: 9780470388372 2 August 22nd, 2009 04:25 AM
Errata - Chapter 9, Page 299, C# RobC BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 August 30th, 2006 03:03 AM
Ch 8. Page 298, 299 brads BOOK: Beginning ASP 3.0 3 September 25th, 2003 01:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.