Topic: Initializations of reference
Author: Kannan Chellappan <chellapk@nmasd.bel.alcatel.be>
Date: 1999/04/02 Raw View
--------------C358747807520295A9E7D722
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi,
I have a question on what will happen to the following code ...
class data;
class base{
public:
data& fn(); // assume there is a correct implementation
....
....
};
class data {
public:
....
....
};
class cursor {
public:
cursor(data&);
};
class user{
public:
user(const base&);
data& d_;
cursor& c_;
...
...
};
user::user(const base& b):
d_(b.fn),
c_(d_)
{
}
According to standards, the order of initialization should be the same
as the order of declaration for non static data members. Hence d_ should
be initialised first and then c_. But, c_ is declared as a reference and
is constructed from b_ (a different type). Is this legal ?
stroustrup says, that(refer to C++ programming language Ed3, sec 5.5
Reference.) The initializer for a "plain" T& must be an lvalue of type
T.
Since c_ is not a constant, and if I interpret the above correctly, then
the above code is illegal.
But my compiler does not flag an error, instead this is what happens :
c_ gets initialised to proper value at times and "junk" value at other
time.
I think the compiler creates a temporary variable, and makes c_ "refer"
to it. So at the end of the constructor, the c_, referes to a memory
that is no longer valid.
Is my interpretation correct ? Any help is appreciated.
Kind Regards
Kannan
[ moderator's note: HTML attachment deleted. Please disable the
feature of your newsreader that attaches an html version
of the submitted article. -sdc ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/04/06 Raw View
In article <370489EB.4D97BE44@nmasd.bel.alcatel.be>,
kannan.chellappan@alcatel.be wrote:
[class definitions snipped for brevity]
> According to standards, the order of initialization should be the same
> as the order of declaration for non static data members. Hence d_ should
> be initialised first and then c_. But, c_ is declared as a reference and
> is constructed from b_ (a different type). Is this legal ?
Sure, why not? Assuming the 'b' referred to in the parameter list does not
get destroyed before the 'user' object, that is.
> stroustrup says, that(refer to C++ programming language Ed3, sec 5.5
> Reference.) The initializer for a "plain" T& must be an lvalue of type
> T.
> Since c_ is not a constant, and if I interpret the above correctly, then
> the above code is illegal.
No. c_ invokes a user-defined conversion constructor, which takes a reference
to a data object. Quite legal.
> But my compiler does not flag an error, instead this is what happens :
> c_ gets initialised to proper value at times and "junk" value at other
> time.
My suspicion would be that the 'base' object gets destroyed before the 'user'
object.
> I think the compiler creates a temporary variable, and makes c_ "refer"
> to it. So at the end of the constructor, the c_, referes to a memory
> that is no longer valid.
No, I don't think the compiler can do that given the code you have provided.
Show us how you create and use the 'base' and 'user' objects. Just a few
lines of code ought to do it.
Jim
Note to recruitment agencies: I will not refer my friends or colleagues
to you nor do I want to use your services to find me a job. I stop
reading unsolicited email as soon as I determine it is job-recruitment
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Kannan Chellappan <chellapk@nmasd.bel.alcatel.be>
Date: 1999/04/06 Raw View
Jim Hyslop wrote:
>
>
> No, I don't think the compiler can do that given the code you have provided.
> Show us how you create and use the 'base' and 'user' objects. Just a few
> lines of code ought to do it.
>
class baseGUI{
public:
int i; // some members
...
...
...
};
class baseGUICom{
public:
baseGUICom(){ b_ = new baseGUI(); }
baseGUI* fn() { return b_; };
baseGUI* b_;
...
...
...
};
class cursor{
public:
cursor(baseGUI& b):b_(b) {};
baseGUI& b_;
...
...
};
class win {
public:
win(baseGUICom* com) : com_(*com), b_(*(com->fn())), c_(b_){}
baseGUICom& com_;
baseGUI& b_;
cursor& c_;
cursor& getC(){return c_; };
};
Here the baseGUICom is created at startup and remains valid till the end
of the application. This baseGUICom creates the baseGUI in one of its
method (not shown here). (And it will definetely be created, before any
operation)
There can be many instances of win. As I had mentioned, the value of
cursor here sometimes is "junk" value. Hence when I perfrom some
opeartions with the cursor object of a win, I get a core dump.
If I do not make c_ as a reference and initialise it as c_(*(com->fn()),
there is no problem.
So my interpretation was that a temporary is being created and c_ made
to refer to that temporary, which(temporary) soon becomes invalid after
the constructor of win.
Also pl. note that none of them is a const reference.
Regards
Kannan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]