473,657 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2123
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<unsign ed 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
4361
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 an object is passed as a parameter to a function it is passed as a reference. Though it would have been fine to pass it as a pointer as well ? is that right ? I had posted a similar question in the C lang newsgroup but did not get much help as C...
7
3015
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 int calcfldpos(int *row, int *column, int *numArray)
12
1746
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
17
3240
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
2
1725
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 and subsubcells are three user-defined types, each of which contains some program logic as well as some drawing capabilities. Since all cells (and subcells and subsubcells) will utilize identical drawing "settings", I'm using a single settings...
11
3343
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 ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
31
3199
by: siddhu | last post by:
why can't we have array of references.
9
1721
by: grocery_stocker | last post by:
How are references and aliases in perl different than references in aliases in C++?
14
1978
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 convert an object of this type to the following type?
22
1653
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 error? Ruben
0
8315
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
8734
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...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7341
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
6172
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
4164
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
2733
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
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
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.