Connecting Tech Pros Worldwide Help | Site Map

Initializing a class member variable which is a reference to an array

amparikh@gmail.com
Guest
 
Posts: n/a
#1: Mar 3 '06
I have some test code which demonstrates the problem. I know I could
solve this by just returning a pointer, but I better use a reference.

In real code, what I actually want to return is a reference to an array
of function pointers. But the code below is good enough to show the
problem.

Thanks.

#define MAX_DEC 11

static char (&MyFunc())[MAX_DEC]
{
static char init[] = "Hihowareya";
return init;
}

class A
{
private :
char (&Ptr)[MAX_DEC];
public:
A() : Ptr(::MyFunc())
{
}

};

int main()
{
A a;
}

The error I get is "'A::Ptr' : initialization of reference member
requires a temporary variable"

Victor Bazarov
Guest
 
Posts: n/a
#2: Mar 3 '06

re: Initializing a class member variable which is a reference to an array


amparikh@gmail.com wrote:[color=blue]
> I have some test code which demonstrates the problem. I know I could
> solve this by just returning a pointer, but I better use a reference.
>
> In real code, what I actually want to return is a reference to an array
> of function pointers. But the code below is good enough to show the
> problem.
>
> Thanks.
>
> #define MAX_DEC 11
>
> static char (&MyFunc())[MAX_DEC]
> {
> static char init[] = "Hihowareya";
> return init;
> }
>
> class A
> {
> private :
> char (&Ptr)[MAX_DEC];
> public:
> A() : Ptr(::MyFunc())
> {
> }
>
> };
>
> int main()
> {
> A a;
> }
>
> The error I get is "'A::Ptr' : initialization of reference member
> requires a temporary variable"[/color]

Yes, Visual C++ is buggy. The code is fine. I suggest you report this as
the ugly bug it is. Ask in 'microsoft.public.vc.language' how to do that.

V
--
Please remove capital As from my address when replying by mail
Alf P. Steinbach
Guest
 
Posts: n/a
#3: Mar 3 '06

re: Initializing a class member variable which is a reference to an array


* amparikh@gmail.com:[color=blue]
> I have some test code which demonstrates the problem. I know I could
> solve this by just returning a pointer, but I better use a reference.
>
> In real code, what I actually want to return is a reference to an array
> of function pointers. But the code below is good enough to show the
> problem.
>
> Thanks.
>
> #define MAX_DEC 11
>
> static char (&MyFunc())[MAX_DEC]
> {
> static char init[] = "Hihowareya";
> return init;
> }
>
> class A
> {
> private :
> char (&Ptr)[MAX_DEC];
> public:
> A() : Ptr(::MyFunc())
> {
> }
>
> };
>
> int main()
> {
> A a;
> }
>
> The error I get is "'A::Ptr' : initialization of reference member
> requires a temporary variable"[/color]

Technically this is a compiler error; the code should compile, and does
compile with g++ and Comeau online.

However, I'd count this compiler error as a blessing... Don't /do/ this
kind of thing!

At the very least, put your array inside a struct (that will probably
make it compile, and also get rid of the lack of a type name). And you
shouldn't really have reference members, they're mostly Evil. But most
of all, unless you're interfacing to C code, an array of function
pointers says you're missing a class with virtual functions, which
should be used instead -- this is C++, and the replacement of arrays
of function pointers, with classes, is the main ++ in C++.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
amparikh@gmail.com
Guest
 
Posts: n/a
#4: Mar 3 '06

re: Initializing a class member variable which is a reference to an array



Victor Bazarov wrote:[color=blue]
> amparikh@gmail.com wrote:[color=green]
> > I have some test code which demonstrates the problem. I know I could
> > solve this by just returning a pointer, but I better use a reference.
> >
> > In real code, what I actually want to return is a reference to an array
> > of function pointers. But the code below is good enough to show the
> > problem.
> >
> > Thanks.
> >
> > #define MAX_DEC 11
> >
> > static char (&MyFunc())[MAX_DEC]
> > {
> > static char init[] = "Hihowareya";
> > return init;
> > }
> >
> > class A
> > {
> > private :
> > char (&Ptr)[MAX_DEC];
> > public:
> > A() : Ptr(::MyFunc())
> > {
> > }
> >
> > };
> >
> > int main()
> > {
> > A a;
> > }
> >
> > The error I get is "'A::Ptr' : initialization of reference member
> > requires a temporary variable"[/color]
>
> Yes, Visual C++ is buggy. The code is fine. I suggest you report this as
> the ugly bug it is. Ask in 'microsoft.public.vc.language' how to do that.[/color]

Ok thanks. Interestingly enough I see this problem on all of them,
VC6.0, VS.NET 2003 and VS.NET 2005.
[color=blue]
>
> V
> --
> Please remove capital As from my address when replying by mail[/color]

amparikh@gmail.com
Guest
 
Posts: n/a
#5: Mar 3 '06

re: Initializing a class member variable which is a reference to an array



Alf P. Steinbach wrote:[color=blue]
> * amparikh@gmail.com:[color=green]
> > I have some test code which demonstrates the problem. I know I could
> > solve this by just returning a pointer, but I better use a reference.
> >
> > In real code, what I actually want to return is a reference to an array
> > of function pointers. But the code below is good enough to show the
> > problem.
> >
> > Thanks.
> >
> > #define MAX_DEC 11
> >
> > static char (&MyFunc())[MAX_DEC]
> > {
> > static char init[] = "Hihowareya";
> > return init;
> > }
> >
> > class A
> > {
> > private :
> > char (&Ptr)[MAX_DEC];
> > public:
> > A() : Ptr(::MyFunc())
> > {
> > }
> >
> > };
> >
> > int main()
> > {
> > A a;
> > }
> >
> > The error I get is "'A::Ptr' : initialization of reference member
> > requires a temporary variable"[/color]
>
> Technically this is a compiler error; the code should compile, and does
> compile with g++ and Comeau online.
>
> However, I'd count this compiler error as a blessing... Don't /do/ this
> kind of thing!
>
> At the very least, put your array inside a struct (that will probably
> make it compile, and also get rid of the lack of a type name). And you
> shouldn't really have reference members, they're mostly Evil. But most
> of all, unless you're interfacing to C code, an array of function
> pointers says you're missing a class with virtual functions, which
> should be used instead -- this is C++, and the replacement of arrays
> of function pointers, with classes, is the main ++ in C++.[/color]

Actually they are pointers to static CreateInstance Functions of
different classes (denoting different h/w types supported) that are
supported.

and the class where this is implemented does the abstraction.
[color=blue]
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?[/color]

Alf P. Steinbach
Guest
 
Posts: n/a
#6: Mar 4 '06

re: Initializing a class member variable which is a reference to an array


* amparikh@gmail.com:[color=blue]
> * Alf P. Steinbach:[color=green]
>> At the very least, put your array inside a struct (that will probably
>> make it compile, and also get rid of the lack of a type name). And you
>> shouldn't really have reference members, they're mostly Evil. But most
>> of all, unless you're interfacing to C code, an array of function
>> pointers says you're missing a class with virtual functions, which
>> should be used instead -- this is C++, and the replacement of arrays
>> of function pointers, with classes, is the main ++ in C++.[/color]
>
> Actually they are pointers to static CreateInstance Functions of
> different classes (denoting different h/w types supported) that are
> supported.
>
> and the class where this is implemented does the abstraction.[/color]

In that case going the object route only buys you a lot of flexibility,
which you might not need, at the cost of at least one indirection.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Closed Thread