473,799 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference trouble in double[3] vs. double*

Hi!

Let's say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn't have, like
a << operator to send it to a stream
*/

private:
double storage[3];
};

I'm having a problem with the subscript operator, I tried

double& Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

and was quite surprised to see it won't compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.

I guess I'm being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like

return (static_cast<do uble*>(&(storag e[0])))[i];

while also making the operator[] non-const, but it sure looks ugly.

So... what is it with the array that doesn't allow what
I'm trying to do? Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).
Jul 22 '05 #1
10 1922

"Jacek Dziedzic" <ja************ *@janowo.net> wrote in message
news:c5******** **@korweta.task .gda.pl...
Hi!

Let's say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn't have, like
a << operator to send it to a stream
*/

private:
double storage[3];
};

I'm having a problem with the subscript operator, I tried

double& Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

and was quite surprised to see it won't compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.
Think about it

const Triplet x;
x[1] = 2.0;

do you really want that to compile?

I guess I'm being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like

return (static_cast<do uble*>(&(storag e[0])))[i];

while also making the operator[] non-const, but it sure looks ugly.

So... what is it with the array that doesn't allow what
I'm trying to do?
Its nothing to do with arrays, returning a non-const reference to any data
member from a const method will not compile.
Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).


Do it like this

double& Triplet::operat or[](const unsigned int i) {
return storage[i];
}

double Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

i.e. define const and non-const versions of your operator[].

john
Jul 22 '05 #2

"Jacek Dziedzic" <ja************ *@janowo.net> wrote in message
news:c5******** **@korweta.task .gda.pl...
Hi!

Let's say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn't have, like
a << operator to send it to a stream
*/

private:
double storage[3];
};

I'm having a problem with the subscript operator, I tried

double& Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

and was quite surprised to see it won't compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.
Think about it

const Triplet x;
x[1] = 2.0;

do you really want that to compile?

I guess I'm being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like

return (static_cast<do uble*>(&(storag e[0])))[i];

while also making the operator[] non-const, but it sure looks ugly.

So... what is it with the array that doesn't allow what
I'm trying to do?
Its nothing to do with arrays, returning a non-const reference to any data
member from a const method will not compile.
Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).


Do it like this

double& Triplet::operat or[](const unsigned int i) {
return storage[i];
}

double Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

i.e. define const and non-const versions of your operator[].

john
Jul 22 '05 #3
John Harrison wrote:
Do it like this

double& Triplet::operat or[](const unsigned int i) {
return storage[i];
}

double Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

i.e. define const and non-const versions of your operator[].


Or

const
double& Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

Jul 22 '05 #4
John Harrison wrote:
Do it like this

double& Triplet::operat or[](const unsigned int i) {
return storage[i];
}

double Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

i.e. define const and non-const versions of your operator[].


Or

const
double& Triplet::operat or[](const unsigned int i) const {
return storage[i];
}

Jul 22 '05 #5
Jacek Dziedzic <ja************ *@janowo.net> wrote in message news:<c5******* ***@korweta.tas k.gda.pl>...
Hi!

Let's say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn't have, like
a << operator to send it to a stream
*/
You should have a declaration in the commented-out section like this:

double const & operator[] (unsigned int i) const;

Please note that a const member function cannot return a non-const
reference. For that reason, you must declare the return type as
'double' or 'double const &'.

Also note that the 'const' for the argument is not the part of the
signature but an implementation detail, because the 'i' that users
pass will be copied to the function.

For that reason, some argue that it shouldn't take part in the
interface. It doesn't matter really because that top-level const
doesn't take part in the signature of the function.

If you need to provide non-const access too, then you can define the
non-const version:

double & operator[] (unsigned int i);

private:
double storage[3];
};

I'm having a problem with the subscript operator, I tried

double& Triplet::operat or[](const unsigned int i) const {
return storage[i];
}


To match the declaration above, the return type must be 'double const
&' here. Cont-qualifying 'i' is ok here because this is the
implementation.

Ali
Jul 22 '05 #6
Jacek Dziedzic <ja************ *@janowo.net> wrote in message news:<c5******* ***@korweta.tas k.gda.pl>...
Hi!

Let's say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn't have, like
a << operator to send it to a stream
*/
You should have a declaration in the commented-out section like this:

double const & operator[] (unsigned int i) const;

Please note that a const member function cannot return a non-const
reference. For that reason, you must declare the return type as
'double' or 'double const &'.

Also note that the 'const' for the argument is not the part of the
signature but an implementation detail, because the 'i' that users
pass will be copied to the function.

For that reason, some argue that it shouldn't take part in the
interface. It doesn't matter really because that top-level const
doesn't take part in the signature of the function.

If you need to provide non-const access too, then you can define the
non-const version:

double & operator[] (unsigned int i);

private:
double storage[3];
};

I'm having a problem with the subscript operator, I tried

double& Triplet::operat or[](const unsigned int i) const {
return storage[i];
}


To match the declaration above, the return type must be 'double const
&' here. Cont-qualifying 'i' is ok here because this is the
implementation.

Ali
Jul 22 '05 #7
Jacek Dziedzic <ja************ *@janowo.net> wrote in message news:<c5******* ***@korweta.tas k.gda.pl>...
Hi!

[redacted]

Try:

class Triplet {
//...
double& operator[](unsigned int i) { return storage[i]; }
double operator[](unsigned int i) const { return storage[i]; }
//...
};

Note the different return types for const vs. non-const.
Jul 22 '05 #8
Jacek Dziedzic <ja************ *@janowo.net> wrote in message news:<c5******* ***@korweta.tas k.gda.pl>...
Hi!

[redacted]

Try:

class Triplet {
//...
double& operator[](unsigned int i) { return storage[i]; }
double operator[](unsigned int i) const { return storage[i]; }
//...
};

Note the different return types for const vs. non-const.
Jul 22 '05 #9
Jacek Dziedzic <ja************ *@janowo.net> wrote:

class Triplet {
public:
Triplet() {/*...*/}
double& operator[](const unsigned int i) const
{ return storage[i]; }
private:
double storage[3];
};

I'm having a problem with the subscript operator, I
was quite surprised to see it won't compile. It works
fine, however, when I change "double storage[3]" to
"double *storage" and allocate it accordingly.


If a Triplet is const, then its members are const. So the original
storage is const and you cannot return a non-const reference to it.
But in the case of "double *storage", "storage" is still const but
the things it points to are non-const.

BCC and GCC 2 give a useful error message for the above code:
In method `double & Triplet::operat or [](unsigned int) const':
warning: conversion from `const double' to `double &' discards const
but GCC 3's output was obfuscated:
In member function `double& Triplet::operat or[](unsigned int) const':
error: could not convert `this->Triplet::stora ge[i]' to `double&'

One solution is to make two operator[] functions, one being const and
returning const ref, and the other non-const and returning non-const ref.
This is a bit inelegant (especially if you want to support volatile
triplets too), I don't know if there is a better solution.
Jul 22 '05 #10

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

Similar topics

9
11917
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is up to which size m would you expect vector<double> returns_by_value() {
10
326
by: Jacek Dziedzic | last post by:
Hi! Let's say I have a class called Triplet that serves as an envelope for double, ie. class Triplet { public: Triplet() {/*...*/} /* some things that double doesn't have, like
4
1186
by: xuatla | last post by:
Hi, I have a class class myType { private: int size; double *elem; .....
2
1053
by: JezB | last post by:
Could someone help ? I'm creating a new project and trying to create a web reference to a web library assembly I've referenced quite happily from other web projects. Trouble is : in my new project it refuses to recognise the reference I've made and gives me the standard error whenever i try to use the reference: The type or namespace 'PresentationFacade' could not be found (are you missing a using directive or an assembly reference?)
51
4477
by: Kuku | last post by:
What is the difference between a reference and a pointer?
5
11362
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
7
6685
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
12
3020
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
9
2003
Steel546
by: Steel546 | last post by:
This program is used to calculate GPA. I'm having trouble actually getting an output. Alright, I KNOW that I don't have an output statement, but I don't know where to put it... heh. Netbeans keeps telling me it's wrong. So, here's my two classes. package KyleTaylorGPA; public class GPA { private double theGPA; private int gradePointsSum;
0
9544
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
10490
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
10259
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
10030
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
9077
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
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...
1
4145
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
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.