LinuxQuestions.org
Review your favorite Linux distribution.
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 07-31-2008, 07:07 AM   #1
sleepyhomme
LQ Newbie
 
Registered: Jan 2006
Posts: 28

Rep: Reputation: 15
C++ Question


I am new to C++. I am very confused is there any difference between the 2 blocks of code below

Code:
template<typename T>
void print(const T& var) {
    std::cout << var << std::endl;
}
and


Code:
template<typename T>
void print(const T &var) {
    std::cout << var << std::endl;
}
My question is the & sign (reference-of operator). Any difference between T& var and T &var?...

Thanks for any help!
 
Old 07-31-2008, 08:10 AM   #2
Nauntilus
Member
 
Registered: Oct 2005
Distribution: All of them
Posts: 140

Rep: Reputation: 18
Yes, it should be &var the & is supposed to go before the variable that it is associated with. also, are you using any uncommon namespaces? because instead of using std::cout you could use just cout if you did this at the beginning of the file.

#include <iostream>
using namespace std;

then you could use just cout and it would save you alot of time typing and far less errors
 
Old 07-31-2008, 08:12 AM   #3
Nauntilus
Member
 
Registered: Oct 2005
Distribution: All of them
Posts: 140

Rep: Reputation: 18
What exactly are you trying to do with that code? because I don't think that that is your only problem, it looks like you are wanting to send two variables into your function, but you need to add a semi-colon ( in between them in the function header.
 
Old 07-31-2008, 08:19 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
The code looks fine to me, T is not a variable but a type specifier (I'm not sure what the proper term is).

As far as T& var and T &var are concerned, I think they mean the same thing. People do the same with pointers (e.g. int* my_pointer or int *my_pointer).
 
Old 07-31-2008, 08:20 AM   #5
sleepyhomme
LQ Newbie
 
Registered: Jan 2006
Posts: 28

Original Poster
Rep: Reputation: 15
I am learning C++ through oopweb.com. The text there are quite good so far. The code i asked is copied from the web below.

http://oopweb.com/CPP/Documents/CPPH...-HOWTO-16.html

I juz dun understand it, that's why i post my question here!
 
Old 07-31-2008, 08:26 AM   #6
Nauntilus
Member
 
Registered: Oct 2005
Distribution: All of them
Posts: 140

Rep: Reputation: 18
There is some more user friendly stuff out there to learn C++ I have tried nearly every website and book that you can buy, lol well not EVERY book or website. But, the best one that I have found is called "Object-Oriented Program Development Using C++" the ISBN is 0-619-15966-9. and this was the easiest for me to learn out of myself. I would definitely check it out if I were you, it is only about $30. and it is far less confusing then that website you were on!
 
Old 07-31-2008, 08:28 AM   #7
sleepyhomme
LQ Newbie
 
Registered: Jan 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks. Let me check whether it's in Safari!
 
Old 07-31-2008, 08:28 AM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
As far as books go, I found Ivor Horton's books to be quite easy to follow.
 
Old 07-31-2008, 08:43 AM   #9
Nauntilus
Member
 
Registered: Oct 2005
Distribution: All of them
Posts: 140

Rep: Reputation: 18
The book I am referring too is actually a college textbook for their first computer science class, and I learned more reading that book then any other book. SO, if you are looking it up on like amazon or half, you might have to go to the text book area.
 
Old 07-31-2008, 10:01 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I thought that this sort of difference was analagous to the difference between:
Code:
char* c;
and
Code:
char *c;
...which makes no difference to the compiled result. For simple statements I think the difference is only really stylistic.
 
Old 07-31-2008, 01:21 PM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
That is correct matthewg42, the reasons for using one form or another normally boil down to the idea of cleanliness when declaring pointers. for example
Code:
char *c,d,e;
//compared to:
char* c,d,e;
Yet this argument is mute really for references as they must be seated when declared.
char a = 56;
char &c,d = a;//invalid

sleepyhomme I have had a look at the reference docs which you are using as a learning aid and would have to reiterate the point that they are not a good learning source. I found a number of incorrect statements and facts, in addition his logic for the function 'zap' is just plain stupid.
See the following for a recommended reading.
http://www.parashift.com/c++-faq-lite/
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

Last edited by dmail; 07-31-2008 at 01:27 PM.
 
Old 07-31-2008, 11:05 PM   #12
sleepyhomme
LQ Newbie
 
Registered: Jan 2006
Posts: 28

Original Poster
Rep: Reputation: 15
OH~ I have also started to read the C++ FAQ last night.
Thank you so much for your recommendation!!!
 
Old 08-03-2008, 06:40 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
It's all in your style of code. I personally place pointer and reference qualifiers next to the variable name because they're associated with the variable, not the type. As was implied above, you get a pointer and an object doing this:
Code:
int* A, B;
but two pointers doing this:
Code:
int *A, *B;
It makes no difference which because the C++ ISO is too freaking huge as it is without arbitrary whitespace restrictions. You could even do this:
Code:
int*A,*B;
which many developers do. Use a style that makes sense to you, but be prepared to learn new styles if you ever plan to work with other developers.
ta0kira

Last edited by ta0kira; 08-03-2008 at 06:44 PM.
 
Old 08-03-2008, 11:21 PM   #14
sleepyhomme
LQ Newbie
 
Registered: Jan 2006
Posts: 28

Original Poster
Rep: Reputation: 15
I get it, man. Thank you
 
  


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
Small question about /etc/rc.local - Symbolic link question Arodef Linux - General 4 05-13-2006 02:29 AM
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
2 part question: Speeding up MDK9.1/GNOME question wardialer Linux - Newbie 6 10-14-2004 03:16 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM

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

All times are GMT -5. The time now is 12:24 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