I am trying to copy the initialization of my class CObjas follows in a function main():
#include <string>
#include <iostream>
class CObj
{
public:
CObj(std::string const& str) : m_str(str) { std::cout << "constructor" << std::endl; }
~CObj() { std::cout << "destructor" << std::endl; }
private:
std::string m_str;
};
int main()
{
CObj obj = "hello";
std::cout << "done" << std::endl;
}
However, the string CObj obj = "hello"does not compile, although it is std::stringimplicitly constructive from a char const*. In my opinion, this should work. Any reason why this is not so? If I do this, it works:
CObj obj = std::string("hello");
source
share