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

  • Home
  • SEARCH
  • 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 8195055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:00:28+00:00 2026-06-07T05:00:28+00:00

better explained by an example: tok.h #include <string> static const char* defaultDelim = .,;;

  • 0

better explained by an example:

tok.h

#include <string>

static const char* defaultDelim = ".,;";

class Tokenizer {
public:
    Tokenizer():
        // 'delim' is the const ref member that is initialized by the temp string 
        delim( (altDelim.size())? altDelim : std::string(defaultDelim) ) 
    {}

    size_t scan(const std::string& str)
    { return str.find_first_of(delim); }

    static void setDelim(const std::string& d) { altDelim = d; }
private:
    static std::string altDelim;
    const std::string& delim;
};

main.cpp

#include <iostream>
using namespace std;

#include "tok.h"

std::string Tokenizer::altDelim;

int main()
{
    Tokenizer tok;

    size_t pos = tok.scan("hello, world");
    cout << pos << endl;
}

the program prints 0 which is wrong. The real code gets a seg fault.

I’d expect that the rule of prolonging the life span of a temp assigned to a const reference would hold here, but apparently it’s not. Do you know the reason’?

  • 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-06-07T05:00:29+00:00Added an answer on June 7, 2026 at 5:00 am

    That rule doesn’t apply to class members. This is stated in 12.2.5 of the C++03 standard:

    A temporary bound to a reference member in a constructor's ctor-initializer
    persists until the constructor exits.
    

    Making the temporary last longer than that would imply that the temporary would have to be kept as part of the class so that its lifetime could be maintained. This would be impossible if the constructor was in a separate compilation unit, because the size of the class has to be known when the class is defined.

    // header file
    struct A {
      A();
      B &b;
    };
    
    
    // file1.cpp
    void f()
    {
      A a; // Reserve the size of a single reference on the stack.
    }
    
    
    // file2.cpp
    A::A()
    : b(B()) // b is referencing a temporary, but where do we keep it?
             // can't keep the temporary on the stack because the
             // constructor will return and pop everything off the stack.
             // Can't keep it in the class because we've already said the
             // class just contains a single reference.
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a basic question, better explained through code: public void checkStatus {
I think this will be better explained with an example I have the following
My question can better be explained via example code. I am using POCO with
World's most convuluted title I know, an example should explain it better. I have
This issue is better demonstrated than explained, so I've set up a git repo
Alex explained what I'm looking for much better than I have: You want an
This is a design question better explained with a Stack Overflow analogy: Users can
To better explain this I'll use Doodle Jump as an example. Assuming that the
To better explain what I'm trying to achieve, here's a simple example: Scenario 1
I think my question is best explained via an example: Suppose I want to

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.