473,698 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference type methods

Why would a method that is defined to return a reference such as with the
operator overload of [], operator[],

href& operator[](int index){
return _array[index];
}

not cause a type mismatch compiler error?

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Jul 14 '08 #1
22 1656
Ruben wrote:
Why would a method that is defined to return a reference such as with the
operator overload of [], operator[],

href& operator[](int index){
return _array[index];
}

not cause a type mismatch compiler error?
Why should it? Assuming _array is an array of href, it just returns a
reference to an href.

--
Ian Collins.
Jul 14 '08 #2
On Mon, 14 Jul 2008 22:11:19 +1200, Ian Collins wrote:
Ruben wrote:
>Why would a method that is defined to return a reference such as with
the operator overload of [], operator[],

href& operator[](int index){
return _array[index];
}
}
not cause a type mismatch compiler error?
Why should it? Assuming _array is an array of href, it just returns a
reference to an href.
hmmm

the symbol array would not be a reference
it would be defined

private:
int _array[default_size];

This comes up from a text book, and I found it puzzling.

Ruben
--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Jul 14 '08 #3
Ruben wrote:
On Mon, 14 Jul 2008 22:11:19 +1200, Ian Collins wrote:
>Ruben wrote:
>>Why would a method that is defined to return a reference such as with
the operator overload of [], operator[],

href& operator[](int index){
return _array[index];
}
}
not cause a type mismatch compiler error?
Why should it? Assuming _array is an array of href, it just returns a
reference to an href.

hmmm

the symbol array would not be a reference
it would be defined

private:
int _array[default_size];
Yes, but _array[index] in an int. I can only guess that href is a
typedef alias for int. The function returns a reference to that int.

--
Ian Collins.
Jul 14 '08 #4
On Mon, 14 Jul 2008 22:30:24 +1200, Ian Collins wrote:
Ruben wrote:
>On Mon, 14 Jul 2008 22:11:19 +1200, Ian Collins wrote:
>>Ruben wrote:
Why would a method that is defined to return a reference such as with
the operator overload of [], operator[],

href& operator[](int index){
return _array[index];
}
}
not cause a type mismatch compiler error?

Why should it? Assuming _array is an array of href, it just returns a
reference to an href.

hmmm

the symbol array would not be a reference it would be defined

private:
int _array[default_size];
Yes, but _array[index] in an int. I can only guess that href is a typedef
alias for int. The function returns a reference to that int.
Ah - your right. I made a mistake and it confused you. I'm so sorry

int& operator[](int index){
return _array[index];
}
But I still am not sure why it doesn't cause a type mismatch error.

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Jul 14 '08 #5
Ruben wrote:
On Mon, 14 Jul 2008 22:30:24 +1200, Ian Collins wrote:
>Yes, but _array[index] in an int. I can only guess that href is a typedef
alias for int. The function returns a reference to that int.

Ah - your right. I made a mistake and it confused you. I'm so sorry

int& operator[](int index){
return _array[index];
}

But I still am not sure why it doesn't cause a type mismatch error.
Because there isn't one.

The type of _array[index] is int. The function returns a reference to
an int, so there isn't any mismatch.

--
Ian Collins.
Jul 14 '08 #6
On Mon, 14 Jul 2008 23:26:07 +1200, Ian Collins wrote:
>}
But I still am not sure why it doesn't cause a type mismatch error.
Because there isn't one.

The type of _array[index] is int. The function returns a reference to an
int, so there isn't any mismatch.
Thanks

That is the part I don't satisfactorly understand. Why is it not
returning the rvalue of the array element.

Why is it different than this

int x, y[10] = {1,2,3,4,5,6,7, 8,9,10};

x = y[3];

I would think the requested return value doen't match the method
definition and I'd have to return something like this

return &i[index];

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Jul 14 '08 #7
Ruben wrote:
On Mon, 14 Jul 2008 23:26:07 +1200, Ian Collins wrote:
>>}
But I still am not sure why it doesn't cause a type mismatch error.
Because there isn't one.

The type of _array[index] is int. The function returns a reference to an
int, so there isn't any mismatch.

Thanks

That is the part I don't satisfactorly understand. Why is it not
returning the rvalue of the array element.
That would be const reference.
Why is it different than this

int x, y[10] = {1,2,3,4,5,6,7, 8,9,10};

x = y[3];
How is it different? It is more like

int& x = y[3];
I would think the requested return value doen't match the method
definition and I'd have to return something like this

return &i[index];
No, you are out by one level of indirection. &i[index] takes the
address of i[index] and yields a pointer to int.

It looks like you should do some background reading on references and
pointers, particularly their differences.

--
Ian Collins.
Jul 14 '08 #8
On Mon, 14 Jul 2008 23:36:45 +1200, Ian Collins wrote:
How is it different? It is more like

int& x = y[3];
>I would think the requested return value doen't match the method
definition and I'd have to return something like this

return &i[index];
No, you are out by one level of indirection. &i[index] takes the address
of i[index] and yields a pointer to int.

It looks like you should do some background reading on references and
pointers, particularly their differences.
agreed. I understand pointers. I've never seen references at all until
C++ and the texts aren't defining them well.

One thing I know is that is an element of an int array is accessed through
either indirection

*(p +4);

or though the array

a[10];

I get a return value of an integer.

But it seems that if I use a reference on the left of the assignment

int& i = a[];
int b[10];

i = b[6];

I seem to get an entirely different behavior.
Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Jul 15 '08 #9
On Mon, 14 Jul 2008 23:36:45 +1200, Ian Collins wrote:
Ruben wrote:
>On Mon, 14 Jul 2008 23:26:07 +1200, Ian Collins wrote:

>>>But I still am not sure why it doesn't cause a type mismatch error.

Because there isn't one.

The type of _array[index] is int. The function returns a reference to
an int, so there isn't any mismatch.

Thanks

That is the part I don't satisfactorly understand. Why is it not
returning the rvalue of the array element.
That would be const reference.
>Why is it different than this

int x, y[10] = {1,2,3,4,5,6,7, 8,9,10};

x = y[3];
How is it different? It is more like

int& x = y[3];
>I would think the requested return value doen't match the method
definition and I'd have to return something like this

return &i[index];
No, you are out by one level of indirection. &i[index] takes the address
of i[index] and yields a pointer to int.
Yes but i[index] returns a literal integer. In fact, I don't know of any
way of returning a varriable, only the data that a symbolic variable
refers to.

I do know how to return an address of a variables assigned space, though a
pointer, like in scanf for example.

return i[index] isn't an lvalue. Yet when the fuction is defined as
returning a reference, it seems to have a seperate syntax unrelated to
the rest of a references implimentation. The only way I know of returning
an actually reference...act ually I don't know a way because even under
this circumstance

int =y =3, z;
int & x = y;

z = x; //STILL an RVALUE assigned and copied to z

so

return x;//should still be an rvalue returned (unless you return a point)

This seems to be a seperate property of references.

Ruben

>
It looks like you should do some background reading on references and
pointers, particularly their differences.
--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Jul 15 '08 #10

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

Similar topics

5
1552
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and if this doesn't see correctly with non-HTML viewers) Ok, I'm fed up with this so I'll explain the situation here and my approach (which sucks), and see if anyone can give a better solution to this. I'm making a way to have several parameters...
13
2187
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
3
5763
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three DLLs. After building it up, I've deployed it on the web server. From my Windows application, I then add a web reference to that web service.
6
1519
by: seeIT | last post by:
In a client application a simple webservice (add/multiply) was added to solution panel but proxy methods do not appear. WSDL description in solution panel follows: <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"...
5
10330
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 <System.Web.Services.Protocols.SoapDocumentMethodAttribute("capeconnect:AppSec:AppSecPortType#getApplicationUsers",
8
10701
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
1395
by: Jared | last post by:
Hi I have two web services running in different locations. I would like to write some code to automatically fail over to the secondary if the primary fails. Rather than duplicating all my code I was hoping there was a simple way to reference different type in a generic way. Is is possible to enter a type as a string and convert it somehow?
2
2016
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. Typically, for one plugin to access a second plugin, I need to set a reference at design-time to the...
1
2743
by: vishnu | last post by:
Hi, I am working on asp.net project which I converted the code fron VB to C# and instead of RaiseEvent in VB code I used the following code. using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security;
13
3022
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody bother in C# with pass-by-reference using the "ref" keyword or "out" for objects in a method parameter list, when, after all, it appears in C# that for all intents and purposes a reference is always being passed, rather than the real object (with, it...
0
8678
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4371
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.