473,811 Members | 2,717 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 1924

"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
11919
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
1187
by: xuatla | last post by:
Hi, I have a class class myType { private: int size; double *elem; .....
2
1054
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
4478
by: Kuku | last post by:
What is the difference between a reference and a pointer?
5
11366
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
6689
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
12
3023
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
2007
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
9605
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
10648
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
10389
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
9205
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...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3018
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.