LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2007, 11:03 AM   #1
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Rep: Reputation: 30
C++ question


Hi. I was going through the code and saw this statement:

Logger &logger;

Can anybody tell me what kind of declaration is that? I've never come across it before.

Thanks.
 
Old 02-20-2007, 11:32 AM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
well that is declaring a reference, but in that context alone it is wrong as a reference always must be initialized;

how about showing a little code that surrounds it..
 
Old 02-21-2007, 03:02 AM   #3
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
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?
 
Old 02-21-2007, 04:21 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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
 
Old 02-21-2007, 04:24 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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)
 
Old 02-21-2007, 04:50 AM   #6
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
>> 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?
 
Old 02-21-2007, 05:01 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-21-2007, 05:11 AM   #8
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Hmm. It makes sense now. Thanks a lot everyone.
 
Old 02-21-2007, 12:28 PM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by dmail
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..

Last edited by xhi; 02-21-2007 at 01:19 PM.
 
Old 02-21-2007, 12:40 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 02-21-2007, 01:18 PM   #11
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by tuxdev
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.
true true. thanks for pointing that out, Tux.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Two things: (Probably Simple) Grub question, and a gDesklets question Wilffo Linux - Software 3 05-20-2006 01:33 PM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 07:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration