472,778 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,778 software developers and data experts.

Trying to return a reference...

char (&)[6] GetArray()
{
static char hello[] = "hello";

return array;
}

int main()
{
char (&hello)[6] = GetArray();
}
Won't compile. Is there anyway around this?
-Tomás
Mar 3 '06 #1
5 1469
"Tomás" <NU**@NULL.NULL> wrote in message
news:un******************@news.indigo.ie...
char (&)[6] GetArray()
char (&GetArray())[6]
{
static char hello[] = "hello";

return array;
return hello;
}

int main()
{
char (&hello)[6] = GetArray();
}
Won't compile. Is there anyway around this?
-Tomás

Mar 3 '06 #2
Tomás wrote:
char (&)[6] GetArray()
{
static char hello[] = "hello";

return array;
}

int main()
{
char (&hello)[6] = GetArray();
}
Won't compile. Is there anyway around this?


Should it be

char ( & GetArray() )[6]
{
static char hello[6] = "Hello";
return hello;
}

You need to learn to read (and form) declarations.

V
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #3
Victor Bazarov posted:
Tomás wrote:
char (&)[6] GetArray()
{
static char hello[] = "hello";

return array;
}

int main()
{
char (&hello)[6] = GetArray(); }
Won't compile. Is there anyway around this?


Should it be

char ( & GetArray() )[6]
{
static char hello[6] = "Hello";
return hello;
}

You need to learn to read (and form) declarations.

Now I've seen everything! I was thinking in my head of all the different
syntaxes it could be, maybe:

char (&) GetArray()[6];

or maybe:

( char & ) GetArray()[6];

or maybe even:

char (&GetArray)()[6];

But the one you suggested, i.e. the correct one, looked way too weird for me
to even fathom it!
-Tomás

Mar 4 '06 #4
> Now I've seen everything! I was thinking in my head of all the different
syntaxes it could be, maybe:

char (&) GetArray()[6];

or maybe:

( char & ) GetArray()[6];

or maybe even:

char (&GetArray)()[6];

But the one you suggested, i.e. the correct one, looked way too weird for me
to even fathom it!
-Tomás


Alternatively, you can resolve to using a simple template:

template <typename T>
struct type
{
typedef T self;
typedef T& ref;
typedef T* ptr;

//...
};

type<char[6]>::ref GetArray(void)
{
static char hello[] = "hello";
return hello;
}

I personally prefer this form because it is easier to read.

Ben
Mar 4 '06 #5
benben posted:
Now I've seen everything! I was thinking in my head of all the
different syntaxes it could be, maybe:

char (&) GetArray()[6];

or maybe:

( char & ) GetArray()[6];

or maybe even:

char (&GetArray)()[6];

But the one you suggested, i.e. the correct one, looked way too weird
for me to even fathom it!
-Tomás


Alternatively, you can resolve to using a simple template:

template <typename T>
struct type
{
typedef T self;
typedef T& ref;
typedef T* ptr;

//...
};

type<char[6]>::ref GetArray(void)
{
static char hello[] = "hello";
return hello;
}

I personally prefer this form because it is easier to read.

Ben

Is there an archive anywhere for little gems like this? I think there
should be! Put stuff like "DefaultInitialised" and "GetArrayLength" in...

-Tomás
Mar 4 '06 #6

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

Similar topics

5
by: John T | last post by:
I am trying to make a function that takes an optional parameter that gets passed by reference. Here is the first line of my function definition: function funQueryDatabase($strQuery,...
5
by: Weston C | last post by:
I'm trying to write a generic/reusable form validator in Javascript... just something that checks to make sure required fields have a value. By generic I mean I don't want to explicitly reference...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
4
by: Deniz Bahar | last post by:
Hello, A couple days ago my friend (OOP guy) shows me what OOP was all about in C++. This morning I figured I can do pretty much the same thing with C (by putting function pointers in...
4
by: Eric A. Johnson | last post by:
For the following code: ' return String representation of CTriangleShape Public Overrides Function ToString() As String ' use MyBase reference to return CShape String Return...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
5
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray)...
7
by: WannabePrgmr | last post by:
What I am trying to do is on the click event of "Command167", run a Dlookup on the number that was just typed into "cboMoveTo1" and find the value located in the table "tblName" in the "Open/Closed"...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.