'Chain member initializers

Is it possible to refer to class members inside "in class initializers"?

Example:

struct Example
{
  std::string a = "Hello";
  std::string b = a + "World";
};

It seems to work (compiles and runs) but is it ok to do?

c++


Solution 1:[1]

#include<bits/stdc++.h>
using namespace std;
struct Example
{
  string a = "Hello";
  string b = a + "World";
};
int main(){
    Example val ;
    val.a = "Salom";
    cout<<val.a<<" "<<val.b; 
}

it is okey, you can do it .

but captures the initial result you have If you give a a new value b will not be updated

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 O'tkir Xo'jayev