Path: utzoo!attcan!uunet!mcsun!hp4nl!dutrun!duttnph!leo From: leo@duttnph.tudelft.nl (Leo Breebaart) Newsgroups: comp.lang.c++ Subject: Simple Question for C++ Experts Keywords: constructors, references Message-ID: Date: 10 Oct 90 14:58:43 GMT Sender: news@dutrun.UUCP Reply-To: leo@duttnph.tudelft.nl Lines: 43 I am troubled by an Annoying Problem. This problem (slightly simplified) boils down to this: I am creating a binary (parse-)tree structure with operators in the inner nodes, and descriptor pointers in the leaves. The constructor: Expr(int leaf) creates an appropriate nil-ary leaf-expression, containing information about, in this case, an integer. The constructor: Expr(String& op, Expr& left, Expr& right) creates a binary internal node. Thus, I am allowed to say 'new Expr(5)' in my code. What I now want to be able to do is to say 'new Expr("+", 5, 6)' as well, and this seems to be impossible - it causes memory faults during run-time. If instead I use the verbose 'new Expr("+", *new Expr(5), *new Expr(6))' then everything works as I want it to. If I understand C++ internal workings correctly, then the short version gets expanded to 'new Expr("+", Expr(5), Expr(6))', i.e. references to temporary storage are passed to my constructor, causing the memory faults later on when the temporaries have been deleted. 1] Is my analysis of the problem correct? 2] Whether it is or not: can I achieve the effect I want in a simple way? I apologize beforehand if this turns out to be too trivial or just a silly error on my part, but I am getting a bit desperate, and I can't find the answer in any of my references (Stroustrup, Lippman, Gorlen). All mailed/posted advice and suggestions welcome... -- Leo Breebaart (leo @ duttnph.tudelft.nl)