Answer the question
In order to leave comments, you need to log in
Why is everything like this in C++?
There will be several questions at once, some of them may relate not only to pluses.
1. Why are const char* and char* types not compatible?
char* foo = "foo";
const char* bar = foo; // Ошибка
int zero = 0;
int zero(0);
int zero = int(0);
int zero{0};
int zero = int{0};
int array[] { 1, 2, 3};
#include <iostream>
class Empty {};
int main(int argc, char** argv)
{
std::cout << sizeof(Empty{});
}
Answer the question
In order to leave comments, you need to log in
https://stackoverflow.com/questions/9834067/differ... the answer to the first question
to your initialization questions
1
2
Well, you can put almost all your questions into Google and it will give you an answer.
1. Const-correctness was invented by Bjarne Stroustrup, and only ≈1999 it was introduced into C without crosses. And since the memory occupied by a string literal cannot be changed (and in modern operating systems, literals sit in a special immutable segment), of course, const char * has become a new type for them. Because C99 allowed writing for compatibility , and C(++) development stalled for a decade, C textbooks took a very long time to write this deprecated line.
3. For small objects, this is the same thing, but for large objects there is a considerable difference. This program compiles starting with C++17.char* text = "foo";
#include <iostream>
class WrapInt
{
public:
int value = 0;
WrapInt(int x) : value(x) {}
WrapInt(WrapInt&) = delete;
private:
};
int main()
{
WrapInt x = 42;
}
int zero = 0;
before C++14, create a temporary object, call the move constructor, and the optimizer does the rest. From C++17, a constructor call if it is not explicit. It seemed to me that in very old versions of DJGPP op= was involved, but XEZ, MinGW and C++03 call the constructor. int zero = int(0);
- which is surprising, the same, only any constructor! Although it looks like an explicit creation of a temporary object. int zero(0);
— constructor call in all versions of C++. int zero{0};
— C++11 universal initializer, also a constructor call. int array[] = { 1, 2, 3};
int array[] { 1, 2, 3};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question