473,582 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is "reference" a (different?) type?

Just wondering about exact terminology used by the standard to
describe a reference. More specifically, is "reference" a type?

int i = 10; // type of i is int

int &ri = i; // ri is declared as a "reference to int". But what is
type of ri? 'int' or 'reference to int'?

-Neelesh

Jul 14 '07 #1
4 1547
On Jul 14, 9:57 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
Just wondering about exact terminology used by the standard to
describe a reference. More specifically, is "reference" a type?

int i = 10; // type of i is int

int &ri = i; // ri is declared as a "reference to int". But what is
type of ri? 'int' or 'reference to int'?

-Neelesh
I believe it is 'reference to int', just like with int *p, where p is
a 'pointer to int'.
And it is a data type, because you couldn't use a int as a function's
argument, if in fact it requires, by declaration, a reference to
<data_typeor a pointer to <data_type>.

Jul 14 '07 #2
On Jul 15, 12:11 am, Thorns <brokenth...@gm ail.comwrote:
>
I believe it is 'reference to int', just like with int *p, where p is
a 'pointer to int'.
And it is a data type, because you couldn't use a int as a function's
argument, if in fact it requires, by declaration, a reference to
<data_typeor a pointer to <data_type>.
That is not correct.

void f(int&); // expects a "reference to int"
void g(const int&) // expects a "reference to const int"

int main()
{
int s = 10;
f(s); // works.
g(10); // works
g(s); // works
f(10); // doesnot work for altogether different reason.
}
-N

Jul 14 '07 #3
Neelesh Bodas wrote:
Just wondering about exact terminology used by the standard to
describe a reference. More specifically, is "reference" a type?

int i = 10; // type of i is int

int &ri = i; // ri is declared as a "reference to int". But what is
type of ri? 'int' or 'reference to int'?
Tricky. Let's see how the use of references influences overload resolution
and the typeid() operator:

#include <iostream>
#include <ostream>
#include <typeinfo>

template < typename T >
struct type_name {

static
char const * value ( void ) {
return ( "unknown" );
}

};

template <>
struct type_name<int{

static
char const * value ( void ) {
return ( "int" );
}

};

template <>
struct type_name<int&{

static
char const * value ( void ) {
return ( "int&" );
}

};

template < typename T >
const char * typeof ( T ) {
return ( type_name<T>::v alue() );
}

int& int_ref ( void ) {
static int i = 5;
return ( i );
}
#define SHOW(expr) std::cout << #expr << " = " << (expr) << '\n'

int main ( void ) {
int i = 10;
int& ri = i;
SHOW( typeof(i) );
SHOW( typeof(ri) );
SHOW( ( &typeid( ri ) == &typeid(int) ) );
SHOW( typeof( int_ref() ) );
SHOW( ( &typeid( int_ref() ) == &typeid(int) ) );
}

Output:
typeof(i) = int
typeof(ri) = int
( &typeid( ri ) == &typeid(int) ) = 1
typeof( int_ref() ) = int
( &typeid( int_ref() ) == &typeid(int) ) = 1
It looks as though ri has type int for all practical purposes. Also,
whatever is returned by int_ref() masquerades really well as an int.
Best

Kai-Uwe Bux
Jul 15 '07 #4
On Sun, 15 Jul 2007 06:08:27 +0200, Kai-Uwe Bux wrote:
>int main ( void ) {
int i = 10;
int& ri = i;
SHOW( typeof(i) );
SHOW( typeof(ri) );
SHOW( ( &typeid( ri ) == &typeid(int) ) );
SHOW( typeof( int_ref() ) );
SHOW( ( &typeid( int_ref() ) == &typeid(int) ) );
}

Output:
typeof(i) = int
typeof(ri) = int
( &typeid( ri ) == &typeid(int) ) = 1
typeof( int_ref() ) = int
( &typeid( int_ref() ) == &typeid(int) ) = 1

It looks as though ri has type int for all practical purposes. Also,
whatever is returned by int_ref() masquerades really well as an int.
Interesting. After initialization a reference is merely an 'alias',
i.e. an 'alternate name', for the referenced object
(http://www.parashift.com/c++-faq-lite/references.html). The type of
the reference becomes indistinguishab le from the type of the
referenced object after initialization.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jul 15 '07 #5

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

Similar topics

2
1574
by: Good Enchiladas | last post by:
While building on a class library for an object model, I get the above error message. The steps to recreate the problem are as follows: 1. Build a RootLevel.dll containing only this code: Public Class Root End Class
2
1672
by: Leicester B. Ford Jr. | last post by:
I want to be able to register a class that a factory can build for me at a later time. How can I pass the class's type to a method? public class MyClass{ static public void Register(Type type) { // do something with: type }
5
2364
by: Lionel | last post by:
Hello all, two quick questions: One: What is the difference between adding a reference to something in my C# project so I can use it (for example, adding a reference to "Microsft.DirectX" when you need to access DirectX functionality in your program) and the "using ..." directive? Two: I've only seen (in books) how to add references to...
0
1933
by: Kaimar Seljamäe | last post by:
Hi, I have to create a web service client which uses SOAP encoding but does not use "multi-reference" values (see http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383513 item 10). If I create SOAP client like this: public class StockService : SoapHttpClientProtocol {
5
10319
by: Mike Logan | last post by:
I used WSDL.exe to generate a client side web proxy for a web service, called the web service, got the results but an array returned by the web service is not in the results. However if I use "Add Web Reference" for the same service the same function works appropriately. Here is the client proxy generated from WSDL.exe ...
1
2999
by: Brian Begy | last post by:
Anyone seen this? "Type mismatch could be due to the mixing of a file reference with a project reference " We have solution we are trying to upgrade to VS 2005. We have about 10 projects in the solution, and cross references are rife. None of the projects refer to each other through file references, yet we receive this error message on...
3
4391
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing...
5
2300
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or System.CodeDom.CodeCastExpression) __problem typecast #1 Desc:i do needed checks but data/commands in XML is dynamic and i don't wanna fix C# code again and...
2
2013
by: Steve | last post by:
Kind of a strange question... I have a VB.NET 2.0 solution containing a main project (my EXE) and a number of other projects (class DLLs) that are "plug-ins" to the main app. These plugins get installed depending on each user's requirements. I'd like to implement a function in one plugin that only executes if another plugin is present. ...
0
7886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7809
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7920
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.