473,385 Members | 1,582 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/pointer to arrays type-cast operators

hi guys,

have you ever tried to create a type-cast operator to a
'reference/pointer to an array'? i hadn't, not until yesterday when i
discovered that it is not exactly the trivial task one'd assume it to
be - none of my apparently naive attempts were accepted for a valid
type-cast operator by any of the compilers i threw them at (i.e. gcc
2.95.3, gcc 3.3.3, vc 2003).
so assume we have:

template < unsigned T >
class A
{
float m[T];
};

which we want to be able to cast to the data type of its sole data
member m. a supposedly correct way would be a member operator:

operator const float (& () const)[T];

which unfortunately ended up in a bunch of syntax errors on all
compilers tested; then the 'out of ideas' attempt:

oprator const float (&)[T] () const;

just got me accused (and rightfully so) of trying to declare an
operator as an array of functions.

so please tell me i was being stupid and i missed something obvious
here.

regards
martin 'blu' krastev

Aug 17 '05 #1
1 2116
martin wrote:
have you ever tried to create a type-cast operator to a
'reference/pointer to an array'? i hadn't, not until yesterday when i
discovered that it is not exactly the trivial task one'd assume it to
be - none of my apparently naive attempts were accepted for a valid
type-cast operator by any of the compilers i threw them at (i.e. gcc
2.95.3, gcc 3.3.3, vc 2003).
so assume we have:

template < unsigned T >
class A
{
float m[T];
};

which we want to be able to cast to the data type of its sole data
member m. a supposedly correct way would be a member operator:

operator const float (& () const)[T];

which unfortunately ended up in a bunch of syntax errors on all
compilers tested; then the 'out of ideas' attempt:

oprator const float (&)[T] () const;

just got me accused (and rightfully so) of trying to declare an
operator as an array of functions.

so please tell me i was being stupid and i missed something obvious
here.


Use a typedef:
--------------------------------------------------------

template<unsigned T> class A
{
float m[T];
public:
typedef const float (&rcfa)[T]; // this should help
operator rcfa() const { return m; }
};

void foo(float const (&rfa)[5])
{
}

int main() {
A<5> a;
foo(a);
A<10> aa;
foo(aa); // error
}
--------------------------------------------------------

V
Aug 17 '05 #2

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

Similar topics

20
by: m sergei | last post by:
Question is about the difference between these two reference to an object versus pointer to an object What are advantages in either one and when would one use them. I see lots of examples when...
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
2
by: Howard | last post by:
Hi, I've got a program whose main object contains an array of "cells", each of which contains an array of "sub-cells", each of which contains an array of "sub-sub-cells". The cells, subcells...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
31
by: siddhu | last post by:
why can't we have array of references.
9
by: grocery_stocker | last post by:
How are references and aliases in perl different than references in aliases in C++?
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
22
by: Ruben | last post by:
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; } not cause a type mismatch compiler...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.