473,748 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5832
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.publ ic.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.publ ic.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
2623
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
2630
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
2115
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 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
6
3369
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
1903
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
1428
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 // OverL2_simple.cpp : main project file.
6
3244
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
4
10541
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 = 100; unionMember.union1.field2 = 200 }; fullStructType var2 = { { unionMember.union2 = 300 } , { unionMember.union2 = 400 } };
13
2338
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 initialization takes place." Does this mean all non-local objects will be zero-initialized before they are initialized by their initializers(if they have)? For example: int g_var = 3; int main() {}
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.