To answer my own question from earlier, What are copy elision and return value optimization?, "If a call to a copy or move constructor is elided, that constructor must still exist and must be accessible. This ensures that copy elision does not allow copying objects which are not normally copyable, e.g. because they have a private or deleted copy/move constructor."
The reason why the initializer list exists is for cases like struct S{ S(int i){} }; which means you require an int to construct an S. If you have a struct A { S s; A() {} }; then that doesn't compile because A::S requires an int to construct and you didn't give it one.
You can't do A() { s = 42; } either because first the s was already constructed when the { is reached and you need to initialize with 42, not assign it. So you need to initialize s before you reach the constructor body.
The way to do it is A() : s(42) {}.
Sometimes you must use the initializer list, sometimes you can simply assign. For consistency people tend to always use the initializer list even if it's not strictly necessary. You eventually get used to the quirky syntax.
user11702787
14:09
is there a way to "translate" C++ Code into Python if you dont know C++ ? or just learn the basic structure ?
If you "translate" C++ code to Python code you get C++ code with Python syntax. It's not very useful. Depending on what you want to achieve there is probably a better way.
Do you want to port a program from C++ to Python or learn C++ or something else?
yeah, it's pretty much always the same, because end() is like a sentinel value for "not valid", because the functions like find need to return something that signals "nothing found"
better representation of ranges without two iterators or a pair<iterator,iterator>, representing generator-like iterators better and not being so very verbose
Hi guys, Im a newbie with c++ and am having a problem.
I have an int in my header file under private and that variable is accessed in another file and is changed. I'm trying to call that same variable to the main function, but it isnt working
Hello people! I've a little code snippet. It'd be great if you could help.
class car
{
public:
int a;
float b;
car(){}
car(int m, int n)
{
a=m; b=n;
disp();
}
void disp()
{cout<<a<<endl<<b<<endl;}
};
class lambo: public car
{ public:
void disp()
{
car(2,5);
//car::disp();
}
};
int main()
{
lambo l1;
l1.disp();
}
it's deleted after the statement is executed, the only thing the statement does is run the constructor. So yes, since the last thing the constructor does is call disp, after that it gets destroyed.
It depends on what you need the value for. You (and everyone else so far) omitted the third alternative:
static const int var = 5;
#define var 5
enum { var = 5 };
Ignoring issues about the choice of name, then:
If you need to pass a pointer around, you must use (1).
Since (2) is apparently...
> (1) cannot be used as a dimension for arrays at global scope; both (2) and (3) can.