473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Difference between variable declarations...

Hi all,

What's the difference between:
int& i;

and

int i;

?

Cheers,

Paulo Matos

Jul 23 '05 #1
11 1215
pmatos wrote:
Hi all,

What's the difference between:
int& i;

and

int i;

?


The former is a reference to an integer, the latter is an integer.

Jul 23 '05 #2
Got it, thanks. 5.5 of C++PL. :)

Jul 23 '05 #3

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d1*************@news.t-online.com...
pmatos wrote:
Hi all,

What's the difference between:
int& i;

and

int i;

?


The former is a reference to an integer, the latter is an integer.


Also former is not legal C++, a reference has to be initialized for sure.

Sharad
Jul 23 '05 #4
Sharad Kala wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d1*************@news.t-online.com...
pmatos wrote:

Hi all,

What's the difference between:
int& i;

and

int i;

?


The former is a reference to an integer, the latter is an integer.

Also former is not legal C++, a reference has to be initialized for sure.


It us legal if it appears inside a class definition.

V
Jul 23 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
Also former is not legal C++, a reference has to be initialized for
sure.
It us legal if it appears inside a class definition.


Yes.
To OP: Even then you need to bind the reference in the initializer list.

Sharad
Jul 23 '05 #6
Sharad Kala wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d1*************@news.t-online.com...
pmatos wrote:
> Hi all,
>
> What's the difference between:
> int& i;
>
> and
>
> int i;
>
> ?
The former is a reference to an integer, the latter is an integer.


Also former is not legal C++,


Wrong. The following should compile:

struct X
{
X(int& i) : i(i) {}

int& i;
};

int main()
{
int i = 3;
X x(i);
}
a reference has to be initialized for sure.


True.

Jul 23 '05 #7

"Rolf Magnus" <ra******@t-online.de> .

Also former is not legal C++,


Wrong.


Not totally wrong.
int main()
{
int& i; // Not legal
}

Given the context in which OP asked the question, it seemed that it was not
in a class definition. The point I was trying to stress was that you cannot
leave the reference uninitialized.

Sharad
Jul 23 '05 #8
Sharad Kala wrote:

"Rolf Magnus" <ra******@t-online.de> .
>
> Also former is not legal C++,
Wrong.


Not totally wrong.
int main()
{
int& i; // Not legal
}


Well, can you name one language construct that can be used _everywhere_ in a
program?
Given the context in which OP asked the question, it seemed that it was
not in a class definition.
Actually, I don't see any context.
The point I was trying to stress was that you cannot leave the reference
uninitialized.


And I agreed to that.

Jul 23 '05 #9
* Rolf Magnus:

Actually, I don't see any context.


Note the semicolon at the end in the Original Posting:

int& i;

This is only valid in a class definition.

But since the question was what does it mean, it probably doesn't
imply very much about context...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #10

Alf P. Steinbach wrote:
* Rolf Magnus:

Actually, I don't see any context.
Note the semicolon at the end in the Original Posting:

int& i;

This is only valid in a class definition.

But since the question was what does it mean, it probably doesn't
imply very much about context...


Indeed, I didn't want to know about context specific questions but I
didn't know it had to be initialized until before reading section 5.5
of C++PL. Anyway, I already understood what it is for. :)

Thanks a lot people and don't get mad about context specific
questions... :)

Cheers,

Paulo Matos
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Jul 23 '05 #11
pmatos wrote:
Thanks a lot people and don't get mad about context specific
questions... :)


Nah, we don't. We're just nitpicking. That's what makes us nerds.
:-)

Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: Arthur Sinko | last post by:
Hi, Is it possible to answer the following question: what is the difference between two declarations: vector<double> a(n); and double a;
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
3
by: albert.neu | last post by:
Hello! What is the difference between "library parts" of C99 and "language parts" of C99. see...
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
11
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.