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 685965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:58:34+00:00 2026-05-14T01:58:34+00:00

In the following code, the object constructed in the last line of ‘main()’, seems

  • 0

In the following code, the object constructed in the last line of ‘main()’, seems to be destroyed before the end of the expression. The destructor is called before the ‘<<‘ is executed. Is this how it is supposed to be?

#include <string>
#include <sstream>
#include <iostream>

using std::string;
using std::ostringstream;
using std::cout;

class A : public ostringstream
{
public:
  A () {}
  virtual ~A ()
  {    
    string s;
    s = str();
    cout << "from A: " << s << std::endl;
  }
};

int
main ()
{
  string s = "Hello";
  A os;

  os << s;
  cout << os.str() << std::endl;

  A() << "checking this";
}

This is the output:

Hello
from A: 0x80495f7
from A: Hello

This is the gdb log:

(gdb) b os.cxx : 18
Breakpoint 1 at 0x80492b1: file os.cxx, line 18. (2 locations)
(gdb) r
Starting program: /home/joe/sandbox/test/os 
Hello

Breakpoint 1, ~A (this=0xbffff37c, __in_chrg=<value optimized out>, __vtt_parm=<value optimized out>) at os.cxx:18
18     cout << "from A: " << s << std::endl;
(gdb) p s.c_str ()
$1 = 0x804b45c "0x80495f7"
(gdb) p *s.c_str ()
$2 = 48 '0'
(gdb) c
Continuing.
from A: 0x80495f7

Breakpoint 1, ~A (this=0xbffff2bc, __in_chrg=<value optimized out>, __vtt_parm=<value optimized out>) at os.cxx:18
18     cout << "from A: " << s << std::endl;
(gdb) p s.c_str ()
$3 = 0x804b244 "Hello"
(gdb) p *s.c_str ()
$4 = 72 'H'
(gdb) c
Continuing.
from A: Hello

Program exited normally.
(gdb)
  • 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-14T01:58:34+00:00Added an answer on May 14, 2026 at 1:58 am

    I believe the behavior that you are seeing is because of the rule that "anonymous temporaries cannot be passed into functions as non-const references" (well not really NOT, but have undefined behavior or different behavior on different compilers). Thus, it does go to the << operator at the last line, but it finds the member(const void*) overload for the << operator rather than the free function (const char*) overload, mainly because of the rule stated above. Thus, a temporary A is constructed, and passed to the << operator which returns a non-const reference.

    Thus, now the operator<<(const void*) is defined as a member of the class while operator<<(const char*) is a free function. When a function is called on a non-const temporary, the only function that matches the argument is looked up in the member functions and no free functions are matched to it. I know for a fact that MSVC has different behaviour to GCC.

    Infact if you try changing the string "checking this" to something smaller so you can see its value (convert it from char* to void* and see what value you get), you will see that what it is printing is actually void* cast of "checking this". So the destructor is called at the very end, but the << has cast the char* to void* and that is what is printed.

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

Sidebar

Related Questions

I am receiving an error on the following line of code: Object reference not
In the constructor of an object I have the following code: ConfigReader::ConfigReader(){ TiXmlDocument doc(CONFIGURATION_FILE_LOCATION);
I have the following code (changed object names, so syntax/spelling errors ignore). public class
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
the following code (from a database object created for each new view): -(void)update:(NSString *)queryText{
The following code gets a JSON object and then spits its contents out into
The following code is not working: private void fileNameLinkButton_Click(object sender, RoutedEventArgs e) { HyperlinkButton
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
The following code should display the id out of the JSON-object, which I got
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is

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.