Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7533055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:37:50+00:00 2026-05-30T05:37:50+00:00

I am trying some code like this //A.hpp class A{ public: A() {} virtual

  • 0

I am trying some code like this

//A.hpp
 class A{
   public:
       A() {} 
       virtual const char *message() const {return "A ERROR";}
 };

 //B.hpp

 #include "A.hpp"

 class B:public A {
   public:
       B() {}
       const char *message() const {return "B ERROR";}
 };

//main.cpp
 #include "A.hpp"
 #include "B.hpp"

void foo(const A& a) {
   /* case 1 */ throw a;   /* (or) */ /* case 2 */ throw B(); // LINE 100
}

int main() {

  B b;
  A &a(b);
  b.message(); // OUTPUT: B ERROR

  try {
      foo(a);
  } catch (const A& a) {
     std::cout<<"EXCEPTION CALLED "<<a.message()<<std::endl;
  }
  return 0;
}

in this case, if I use
case 1:
throw a; //a is an reference of B b;
OUTPUT: A ERROR

case 2:
throw B(); // creates new B;
OUTPUT: B ERROR

What I don’t understand is, why doesn’t there is no consistency betweek two cases,

If you pass by reference, all the time, there should be some consistency,
If I create a new variable inside the function, that is called in the try block, then it calls the correct virtual method, else it doesn’t….. Can anyone tell me the control flow…. Please advise….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:37:53+00:00Added an answer on May 30, 2026 at 5:37 am

    Because an object is copied before thrown.

    Even if the parameter a of foo points to an instance of B at run-time, what matters is the compile-time type of the throw expression. So, effectively, the instance of B is passed to the copy-constructor of A (which is legitimate since B inherits A) and a new A instance created and then thrown.

    The reason for copy is that compiler must guarantee the lifetime of the exception object as long as there is any catch block that could catch it. So, it can’t risk a stack object “falling off the stack edge” or heap object being deallocated by some destructor invoked during stack unwinding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to write some code that looked like this: public List<IObject> getObject(){
I'm trying to parse some bbc code like this: [item=1]a[/item] [item=2]b[/item] [item=3]c[/item] I'm able
I have some code like this: public void SaveImage(int Counter) { var task =
I'm trying to unit test some code that looks like this: def main(): parser
I have some code like this trying to get some data from a Documents
I am trying some simple code on fork. When I give code like this,
I have a javascript file which has some code somewhat like this: var Slide
I've got some Perl code like this: my $match = $matches{$key} ? $matches{$key} was
I have some code that looks like this: int i = 0; foreach (var
I am trying to put a value within some javascript code like so: <?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.