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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:44:35+00:00 2026-05-26T15:44:35+00:00

Gotw 80 includes the following example: // Example 1 // #include <string> using namespace

  • 0

Gotw 80 includes the following example:

  // Example 1
  //
  #include <string>
  using namespace std;

  class A
  {
  public:
    A( const string& s ) { /* ... */ }
    string f() { return "hello, world"; }
  };

  class B : public A
  {
  public:
    B() : A( s = f() ) {}
  private:
    string s;
  };

  int main()
  {
    B b;
  }

The article goes to discuss why the line s = f() is incorrect – due to object lifetimes and order of construction. The article states that at the time, the error wasn’t picked up by the compiler.

However, ignoring the problems of order of intialisation and object lifetime, I don’t see how s = f() in the parameter list of the constructor can be legal syntactically – it appears to be trying to initialise a member in the parameter list (or perhaps declaring a default value). Can anyone explain what this syntax is trying to do?

  • 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-26T15:44:36+00:00Added an answer on May 26, 2026 at 3:44 pm

    Syntactically it’s legal… when you have a base class with a constructor that takes arguments you can of course pass any expression as parameter:

    strut A {
        A(int) {}
    };
    
    struct B : A {
        B() : A( any expression that returns an int ) {}
    };
    

    The problem is that when evaluating the expression in the example the object is not even yet a fully constructed A instance, so that code is invalid for two distinct reasons:

    1. Calls a method of A of an non-instance (the constructor didn’t start yet): f() call is illegal.
    2. Assigns to a member that hasn’t been initialized: s=... is illegal.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From Herb Sutter's GotW #6 Return-by-value should normally be const for non-builtin return types.
There is a example in http://www.gotw.ca/gotw/067.htm int main() { double x = 1e8; //float
Referring to article Gotw 54 by HerbSutter, he explains about The Right Way To
I am trying to come up with a case insensitive string, and I found
Initially I was trying to typedef a template class and I got to the
Is their a way to use a non-member non-friend function on an object using
According to Herb Sutter the code below wouldn't compile. See this site http://www.gotw.ca/gotw/066.htm from
Assume I want to define two variables of class {Type}. The constructor takes 1
I'm wanting a non-reference counted smart pointer that can combine some of the useful
Coming from a C++ background, this came as a surprise to me. In C++

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.