468,249 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

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 1370
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Weston C | last post: by
25 posts views Thread by cppaddict | last post: by
8 posts views Thread by Rich Grise | last post: by
4 posts views Thread by Deniz Bahar | last post: by
4 posts views Thread by Eric A. Johnson | last post: by
5 posts views Thread by comp.lang.php | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.