473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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"

Mar 3 '06 #1
5 5805
am******@gmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #2
* am******@gmail.com:
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: 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?
Mar 3 '06 #3

Victor Bazarov wrote:
am******@gmail.com wrote:
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.

V
--
Please remove capital As from my address when replying by mail


Mar 3 '06 #4

Alf P. Steinbach wrote:
* am******@gmail.com:
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: 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?


Mar 3 '06 #5
* am******@gmail.com:
* Alf P. Steinbach:
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.


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?
Mar 4 '06 #6

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

Similar topics

3
by: J. Campbell | last post by:
I have a class that I want to contain a const data-member equivalent to eg: int array = {7,11,13,17,19,23,29,31}; What's the simplest way to create such a table inside a class? Thanks, Joe
2
by: chris | last post by:
Hi all, I need to pass reference to a class via constructor, to initialize its member variable. This is the code simplified : class Logger{ public : Logger(string filename);
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
3
by: raylopez99 | last post by:
Hi all, I'm getting a runtime error for the below program, though it compiles fine. The culprit seems to be the managed array--it doesn't exist. What am I doing wrong? RL //...
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.