Hi, I have a few yes/no questions about the code line below:
1 2 3
Cat acCats[] = { Cat("Fred"), Cat("Tyson"), Cat("Zeke") };
//Cat is a user defined class with no overloading of the subscript[] operator
//Cat has the following constructor: Cat(std::string strName)
Questions (seeking confirmation on statements below):
1) The code above is performing an initialization (i.e., defining acCats and giving acCats initial values at the same time)
2) Cat("Fred") is an anonymous object created using the class constructor Cat(std::string strName), it has expression scope and will go out of scope/be destroyed before Cat("Tyson") is instantiated
3) B/c we are performing an initialization, the compiler then calls the default copy constructor of Cat to create a Cat object at acCats[0] (we are not using the assignment operator here)
4) acCats[0] is not the same object as Cat("Fred"), in other words, we’ve not somehow extended the life of Cat("Fred") (unlike how a const reference to a literal value would extend the life of that literal value)
5) would the following extend the life of the anonymous object Cat(“Fred”)?
I will test the last line above in the mean time, google results seem to indicate that one cannot have an array of references
Thanks, and please delete this thread if it is against rules (before posting, I used search terms such as: constructor, anonymous object, and array but couldn't confidently reach a solution; I think my questions are too basic)
I'll ignore the over-reaction but the website link could have been a very useful adjunct to the OP but for whatever reason it wasn't there.
As a tip I suggest getting some practice at C++ and its intricacies and write a short 5 or 10 line program and test out the ideas and questions raised. Often people are looking for a quick fix and it's counterproductive. Someone will more than likely come along and give the specific answer but my bet is you won't learn anywhere as much as just trying it out, which a lot of contenders here won't do.
if you even looked at the link, you'll have found the origin of the questions, along with the work done to be able to even produce those questions, hint hint, look at the comments, and that's more hint than you gave the OP
perhaps the authentic experience you're craving for is one where OP asks a generic question, then ever so slowly, like squeezing toothpaste, narrow it down to the specifics, all of which the OP has already done for you, so that a jaded 1924 poster wouldn't feel compelled to give a run-around under the guise of "learning more"
it is indeed "learned" that you think a beginner ought to jump through more hoops b/c you've decided that they're lazy
reread OP's questions, and give an example of the short 5 or 10 or 20 line program that the OP ought to be able to come up with to answer the type of questions listed
> 2) Cat("Fred") is an anonymous object created using the class constructor Cat(std::string strName)
Yes.
> it has expression scope
There is no scope called expression scope. Scopes are relevant only for names that are introduced in a C++ program; Cat("Fred") represents an unnamed (anonymous) object.
Scopes in C++: http://en.cppreference.com/w/cpp/language/scope
> will go out of scope/be destroyed before Cat("Tyson") is instantiated
Cat("Fred") will be constructed before Cat("Tyson"). After the full statement is evaluated (after the array acCats is initialised) , the temporary objects are destroyed in reverse order of construction.
Temporary object lifetime: http://en.cppreference.com/w/cpp/language/lifetime
> B/c we are performing an initialization, the compiler then calls the default copy constructor of Cat to create a Cat
> 5) would the following extend the life of the anonymous object Cat(“Fred”)?
> ...
> google results seem to indicate that one cannot have an array of references