0

C++ novice here, it's probably a simple thing, but I am trying to do something like this, I have a class

class Foo 
{
    private:
        Bar m_bar;
}

class Bar 
{
    explicit Bar(std::string param);
    Bar(Bar const &) = delete;
    void operator=(Bar const &x) = delete;
}

Foo::Foo()
{
    // how do I initialize this?
    m_bar = Bar("new value"); // doesn't work as = and copy constructor are deleted
}

I want to initialize m_bar, how do I do it? I am learning C++ coming from Java and these things are hard to understand even after reading/trying to search for answers here.

273K
  • 29,503
  • 10
  • 41
  • 64
Roman Goyenko
  • 6,965
  • 5
  • 48
  • 81
  • 2
    You need to use the initialiser list https://en.cppreference.com/w/cpp/language/constructor – Alan Birtles May 11 '23 at 20:44
  • 1
    Deleted copy constructor and assignment operator aren't actually all that relevant here, the bigger issue is that your `Bar` has no default constructor. Alan's suggestion is the correct remedy for this case. – Nathan Pierson May 11 '23 at 20:44

0 Answers0