D
D
Daniil Demidko2016-03-08 04:11:01
C++ / C#
Daniil Demidko, 2016-03-08 04:11:01

Design magic?

struct WorkExample
{
    WorkExample (const int &number ){}
};

WorkExample wOne(1); // ok
WorkExample wTwo=1; // ok

struct Example
{
    Example ( const std::string &line ){}
};

Example one("line"); // ok
Example two="line"; // error

Why is it not possible to call the Example constructor immediately after the declaration with = ?
If you replace const std::string &linewith const char *const line, everything works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Batalin, 2016-03-08
@Daniro_San

The compiler does not do more than one conversion
If we replace with Example two=std::string("line");, then everything will work

Example two="line";  ->  Example two("line"); //Нет конструктора, который принимает const char*

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question