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

A

amparikh

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"
 
V

Victor Bazarov

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"

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
 
A

amparikh

Victor said:
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"

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.

Ok thanks. Interestingly enough I see this problem on all of them,
VC6.0, VS.NET 2003 and VS.NET 2005.
 
A

Alf P. Steinbach

* (e-mail address removed):
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"

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

amparikh

Alf said:
* (e-mail address removed):
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"

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++.

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.
 
A

Alf P. Steinbach

* (e-mail address removed):
* Alf P. Steinbach:

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top