|
-
September 9th, 2002, 05:15 PM
#1
Reference Variable As Class Member
How do I initialize a reference variable declared inside a class or struct? I need a simple code example.
-
September 9th, 2002, 05:42 PM
#2
Use the constructor initializer list:
Code:
class A
{
public:
A(int& ref)
: m_ref(ref)
{
}
private:
int& ref;
};
Jeff
-
September 9th, 2002, 09:36 PM
#3
Thank you. I am ashamed to admit that I have never seen this before.
-
September 9th, 2002, 10:18 PM
#4
It really doesn't happen that often. Now you must guarantee that the lifetime of the passed-in parameter is at least as long as the class instance. It's usually not worth the trouble. At least with a pointer, it can be set to NULL.
I actually had to compile this example to ensure myself this was how it really works. So, no reason to be ashamed 
Jeff
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|