473,386 Members | 1,958 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,386 software developers and data experts.

confused by references



I'm confused by the following. I'd appreciate any advice.
//-------------------main.cpp

#include <iostream>
using namespace std;

class A
{
public:

int val;

A(int v):val(v){}
};

/////////////////////////////////////////////
class MyClass
{
private:

A& a;

public:

MyClass(A& a_):a(a_){}

int Get(){return a.val;}

};

///////////////////////////////////////////
int main()
{
MyClass mc(A(1));

cout << "\nmc.a is now " << mc.Get();

//how can mc.a be pointing to a valid integer? Isn't A(1) a
temporary?

return 0;
}
Also, if the ctor of MyClass is changed to

MyClass(A a_):a(a_){}

I get the expected result (the reference is pointing to gobbldygook).
I don't understand why, given the behavior of the first example, this
should be so.

thanks
Jul 22 '05 #1
1 1425
tarmat wrote in news:07********************************@4ax.com:
class MyClass
{
private:

A& a;

public:

MyClass(A& a_):a(a_){}

int Get(){return a.val;}

};

///////////////////////////////////////////
int main()
{
MyClass mc(A(1));

cout << "\nmc.a is now " << mc.Get();

//how can mc.a be pointing to a valid integer? Isn't A(1) a
temporary?

return 0;
}
Also, if the ctor of MyClass is changed to

MyClass(A a_):a(a_){}

I get the expected result (the reference is pointing to gobbldygook).
I don't understand why, given the behavior of the first example, this
should be so.


Your first example relies on a *non-standard* feature. you are binding
a non-const reference to temporary, MS VC6 allows this and so do other
compilers that choose to operate in a VC6-compatible mode, you may be
able to give your compiler an option to increase conformance to The
C++ Standard (for e.g /Za (MS VC7.1), -pedantic and -ansi (gcc)) also
turn your compiler warnings all the way up (often -Wall).

Once you change MyClass to:

class MyClass
{
private:

A const & a;

public:

MyClass(A const & a_):a(a_){}

int Get(){return a.val;}

};

Then your example will be standard conforming, however it will also
exhibit "Undefined behaviour" (aka UB). As you correctly sumize the
temporary is destroyed at the end of the statement "MyClass mc(A(1));".
This means that its destructor (if any) will be called and the compiler
may reclaime or reuse the sorage it occupied.

In this case *Nothing* happens, the compiler doesn't release or reuse
the storage for your temporary and A's destuctor does nothing.

Note the above paragraph is pure speculation, once you write code that
exhibits UB the C++ Standard washes it hands of *all* responsability
for what happens.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nfr | last post by:
Default build directories seem to be somewhat different between C# and Visual Basic.NET. Both have "Debug" and "Release" directories under "obj". However, Visual Studio.NET defaults to a single...
3
by: Barry Mossman | last post by:
Hi, I get the feeling that I am missing something with regards to casting. The CopyTo method allows me to copy the contents of a collection into an array. My collection is a MatchCollection...
1
by: EO | last post by:
I am trying to use the MSFT data access application block on 3 machines. Machine 1: Sandbox environment; I installed the application block with the msi. The Sqlhelper class compiles & runs...
1
by: Leo Muller | last post by:
Okay, I got confused here. In order to get the messages out the MSMQ 3, on windows 2003, I need to invoke a COM component (ProgID). However, if I compile a class of .NET, and get a DLL, is this...
3
by: C CORDON | last post by:
I am verry confused about classes. I understand that classes can encapsulate properties, methods, events (don't know hoy to add events to a class), etc. I am confused with this: if you can...
13
by: Marvin | last post by:
Hi: I have been programming with OOP and C++ for over 10 years but I am new to javascript. I am very confused about one aspect and would appreciate it if someone could explain the differences...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
10
by: Ruan | last post by:
My confusion comes from the following piece of code: memo = {1:1, 2:1} def fib_memo(n): global memo if not n in memo: memo = fib_memo(n-1) + fib_memo(n-2) return memo I used to think that...
10
by: vb.here | last post by:
Hi all, I am new to C++ and was just reading about polymorphism. I tried to write a very simple program. Then a curious thought came into my mind. And instead of using pointer in polymorphism, i...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.