Is this a bad practice? C ++

Class1 myclass(someparameter);

int main(int argc, char* argv[])
{
    myclass = Class1(anotherparameter);
}

I have a variable in the file area.

I do not know how to talk about my question. But basically I copy the class, and this code looks pretty scared. Are there any implications for this? Should I use new/ deleteinstead? The potential problem I can think of is that the class contains pointers (but then this could be solved by creating a copy constructor)

+3
source share
3 answers

Assuming your class can copy correctly, I don't see anything wrong with that. But this is not so effective, since myclass is actually initialized twice: once when it is declared, and again where you assigned it to another instance.

, , . .

+1

, . , " ". ...

+2

, , , . - , . ... , ( operator=), . , , , , .

:

std::string sep = "\n";

int main(...)
{
    if (...)
        sep = std::string("\r\n");
}

, std::string , operator=(const char*). .

0
source

Source: https://habr.com/ru/post/1784888/


All Articles