473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1838
> 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.