Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datamember as reference in a c++ class

Status
Not open for further replies.

enigmatrix

Programmer
Aug 24, 2000
8
0
0
IN
I want to use reference variable as a datamember in a c++ class,but during the compilation it is showing an
error: "refence member requires an initializer.."

Program i wrote is:


#include <iostream.h>

extern int pub;
class ex
{
int &data;
public:
ex()
{
data=::pub;
}
};

int pub=200;
void main()
{
ex a;
}
****
Anybody body knows technique to use a reference as datamember.
****

 
reference members must be initialized in the initialization list, i.e.:

extern int pub;
class ex
{
int &data;
public:
ex():data(pub)
{
}
};

Hope this helps
-pete

[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top