dimanche 20 septembre 2020

Use of c++ lambda expression to initialize a class member

I want to initialize one class member (std::string filePath) using lambda expression. Program is compiling fine however there is no output. What is wrong here?

#include <iostream>
#include <string>

class MyString
{
  public:
  std::string filePath = [this] ()
  {
      return oneStr.append(" two");
  }();
    
  std::string oneStr;
};

int main()
{
  MyString str;
  str.oneStr = std::string(" one");
  printf("%s", str.oneStr.c_str());
}

Aucun commentaire:

Enregistrer un commentaire