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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a ref to overriden constructor 1

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
Can anyone help me with this? I have in my declare:

Code:
CheckBookSet  & m_CBSet;
AddItemDlg(CWnd* pParent = NULL);   

AddItemDlg(CWnd* pParent, CheckBookSet & CBSet, int line);
then the constructor:
Code:
AddItemDlg(CWnd* pParent, CheckBookSet & CBSet, int line);
{
m_CBSet = CBSet;
}
I get double errors, "I must ini reference in consructor" and also m_CBSet = CBSet "= operator is not available.

Can anyone help me out? I can make it work by passing CheckBookSet as a pointer to my overriden constructor, but I would like to pass by ref. Is this possible? Smart?

Thanks in advance for any help you can give me.

CJB
 
The first error may be because of the fact that u r not initializing the reference variable in the first constructor.

I am not sure about the second error, but i think u will have to operator=() for the CheckBookSet class/struct.
 
Yes, when you have member variable declared as a reference it must be initialized in all constructors. And 2nd problem : you cannot initialized reference in the constructor body.
Example: Do it this way

Code:
class Test
{
public:
	Test( int& i )
	: m_i( i )
	{}

private:
	int& m_i;
};

not this way...

Code:
class Test
{
public:
	Test( int& i )
	{
		m_i = i;
	}

private:
	int& m_i;
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top