By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

dangling reference

P: n/a
I saw this in some code I'm maintaining. Is it a bad idea?
class T
{
public #:
T () i_mem(0)
{ }

T (T2 &t2) i_mem(0), t2_ref (t2)
{ }
private:
int i_mem;
T2 &t2_ref;
}

This is from memory and the real code compiles so any syntax errors
are my fault. It also had destructors, copy constructors etc.

What bothered me was the apparently uninitialised reference in the
default constructor.

If it were a pointer then would it be initialised to NULL by default?

But a reference *must* reference something?
--
Nick Keighley

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> class T
{
public #:
T () i_mem(0)
{ }

T (T2 &t2) i_mem(0), t2_ref (t2)
{ }
private:
int i_mem;
T2 &t2_ref;
}

This is from memory and the real code compiles so any syntax errors
are my fault. It also had destructors, copy constructors etc.


This code should not compile, because the all member references must be
initialized in constructor initializer list. What might be happening in your
case (you say it compiles) is that class T is actually a template
parametrized on T2 type. If so, you might not get error unless default
constructor is actually used. I don't know if this is mandated by the
standard, but at least Visual C++ .NET behaves this way.

cheers,
Marcin

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.