473,385 Members | 1,727 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,385 software developers and data experts.

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::operator[](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<double*>(&(storage[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 1855

"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::operator[](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<double*>(&(storage[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::operator[](const unsigned int i) {
return storage[i];
}

double Triplet::operator[](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::operator[](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<double*>(&(storage[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::operator[](const unsigned int i) {
return storage[i];
}

double Triplet::operator[](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::operator[](const unsigned int i) {
return storage[i];
}

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

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


Or

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

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

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

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

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


Or

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

Jul 22 '05 #5
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
*/
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::operator[](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.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
*/
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::operator[](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.task.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.task.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::operator [](unsigned int) const':
warning: conversion from `const double' to `double &' discards const
but GCC 3's output was obfuscated:
In member function `double& Triplet::operator[](unsigned int) const':
error: could not convert `this->Triplet::storage[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
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::operator [](unsigned int) const':
warning: conversion from `const double' to `double &' discards const
but GCC 3's output was obfuscated:
In member function `double& Triplet::operator[](unsigned int) const':
error: could not convert `this->Triplet::storage[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 #11

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

Similar topics

9
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...
10
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
by: xuatla | last post by:
Hi, I have a class class myType { private: int size; double *elem; .....
2
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...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
5
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...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
12
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....
9
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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,...
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
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,...

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.