Constructor and Anonymous Objects

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”)?

 
Const Cat &acCats[] = { Cat("Fred"), Cat("Tyson"), Cat("Zeke") };


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)
Last edited on
closed account (48T7M4Gy)
Sounds like a well written homework exercise.
Perhaps the yes/no format was not appreciated.

Or that it was too considerate to identify the exact points of confusion.

Have a peek at the source in question:

http://www.learncpp.com/cpp-tutorial/121-pointers-and-references-to-the-base-class-of-derived-objects/

Perhaps there was too much homework done following the "read before post" sticky.
Last edited on
closed account (48T7M4Gy)
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.

PS const, not Const
you mean the link OP provided you?

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
> 1) The code above is performing an initialization

Yes. Aggregate initialization http://en.cppreference.com/w/cpp/language/aggregate_initialization


> 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

Copy initialisation of the Cat objects in the array is performed.
In this case, the anonymous temporary object is an rvalue; the move constructor would be used for Copy initialisation
Copy initialisation: http://en.cppreference.com/w/cpp/language/copy_initialization
Note: Copy-elision may be (usually would be) applied http://en.cppreference.com/w/cpp/language/copy_elision
Note: Copy-elision is optional in C++14; it would be mandatory in C++17.


> 4) acCats[0] is not the same object as Cat("Fred")

Yes. acCats[0] is an lvalue; Cat("Fred") is a prvalue http://en.cppreference.com/w/cpp/language/value_category


> 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

We can only have arrays of objects; references are not objects.
Object: http://en.cppreference.com/w/cpp/language/object
Thank you for the details and the attached references; it was more than what one could hope for

I will continue past learncpp.com to read "C++ primer" and "C++ primer plus"
Last edited on
Topic archived. No new replies allowed.