ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, I don't understand what declaring a reference like that means. I mean I don't understand what happens internally. It's not a function argument, if that's what you're wondering. It's a private variable of a class. What possible context could it be in?
Well if its a member then it is valid and the constructor will initialise it. A reference is like constant pointer to a type(on steroids); yet it does not use the pointer syntax. http://www.parashift.com/c++-faq-lite/references.html
When an object of the class (that this member variable belongs to) is created the constructor will be required to create an instance of the Logger class. The private member variable logger will then be assigned to the instance of this object.
Think of a reference of an object the same as a pointer to an object. The big difference is that whilst a pointer can point to different objects the reference is fixed to the same object for the life of the reference (in this case the life of the object)
>> The private member variable logger will then be assigned to the instance of this object.
Shouldn't it be the other way round? The constructor creates an instance of the Logger class and stores its reference in this variable?
If the above is correct, does this mean that this kind of reference is used to make sure that a particular member variable is initialised whenever an instance of the containing class is created?
Yes you are correct, sorry I wasn't very clear with my wording there.
This technique is used to ensure that a logger instance exists for the duration of the object.
I guess that it's not the most common technique to achieve this type of guarantee, you would probably more often see the use of singleton classes, which use a static variable and thus the object is available for the duration of the program.
Well if its a member then it is valid and the constructor will initialise it. A reference is like constant pointer to a type(on steroids); yet it does not use the pointer syntax. http://www.parashift.com/c++-faq-lite/references.html
a reference has to be initialized. so even if it is a member, the initial post is still wrong.
the only way a ref can stand alone is as a parameter.
edit> i stand corrected. as dmail pointed out the ref type can be initialized in the initializer list of the constructor..
It is not wrong to have a member reference. The reference must be initialized by the constructor in the initializer list, which satisfies the "all references must be initialized" requirement.
It is not wrong to have a member reference. The reference must be initialized by the constructor in the initializer list, which satisfies the "all references must be initialized" requirement.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.