473,466 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

dangling reference

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
1 1839
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: kaede | last post by:
Hi all, I have a quick question regarding binding temporary to const reference. I wrote a little toy program to test it, but I don't seems to understand the behaviour. Consider the following...
13
by: Aravind | last post by:
I would like to know in what manner dangling pointers affect the security of a application developed using C++.What are the loopholes that are created by dangling pointers and how they could be...
5
by: Hans Van den Eynden | last post by:
I thought that integers resized on the stack and that a value type was just defined in the scope of the function. And when the function returns the local variables were removed from the stack. But...
11
by: John | last post by:
Hi: Below is a simple code: class link1 { public: link1(); link1(int &b1, double &b2); int* a1;
6
by: Tony Johansson | last post by:
Hello Experts! I'm reading in a book about C++ and the book says common errors is "A function should not return a constant references to its parameter passed by constant references" Why? Here...
6
by: Matthias Kaeppler | last post by:
Hi, I have a question regarding references, and their chance to "dangle" (if that's possible at all): Say I have a collection ob objects, and I take a reference to one of them. Now I sort...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
10
by: Belebele | last post by:
Suppose that I have a method that returns references to "elements" in an iterator, and a method to advance the iterator: class Element { /* ... */ }; class Iterator { public: Element&...
1
by: sridhard2406 | last post by:
Hi All, I have a doubt on undrestanding Dangling pointers.Below I mentioned sample code. please let me know, my view on Dangling pointers is correct or not? main( ) ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...

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.