Subj : Re: works only outside class To : borland.public.cpp.borlandcpp From : Gerhard Wolfstieg Date : Wed Dec 24 2003 04:43 pm Thank you for your response in this difficult problem. I should have mentioned that following line is included (my view was to close): typedef unsigned long term_t; and I add here that the Pl_ function are located in a VC DLL. (it is SWI-ProLog - I successfully ported the whole into one BC exe file but I don't have the machine in a BC dll yet) The source is near by the minimum -- and a testing frame. I only left out try and catch and a getchar to have the chance to see something without setting breakpoints. gw Thomas Maeder [TeamB] wrote: > Gerhard Wolfstieg writes: > >> Which kind of reasons are conceivable in the following case? > > There is not much difference in the code you show. I'm just giving an > educated guess here. If I guess wrong, I'm afraid you'll have to reduce > your program to the absolutely minimal size that still causes the > behavior you see (<50 lines) and post the result. > > >> This code is running fine: >>>> >> int >> main( int, char *[] ) >> { >> ... >> term_t ref = PL_new_term_ref(); > > This initializes ref with the return value of PL_new_term_ref(). In > principle, the term_t copy constructor is used for this, but it could also > be be optimized away. > > >> if( !PL_chars_to_term( "x( A, 2 )", ref ) ) >> throw RC_Syntaxfehler; >> ... >> } >> << >> The next does not work (it seems to loop in a second thread): >>>> >> struct gwTerm >> { term_t ref; >> >> gwTerm( char const * ); >> }; >> >> gwTerm::gwTerm( char const *text ) > > Now ref is first default_constructed, since you don't explicitly > initialize it in the member-initializer list. > > >> { ref = PL_new_term_ref(); > > Then you copy-assign the return value of PL_new_term_ref() to ref, using > term_t's copy-assignment operator. > > > If term_t's copy constructor and copy-assignment operator are correctly > written (or generated), the second version should only be less efficient > than the first. > > > To cause ref to be initialized in the second version like it is > initialized in the first version, change the constructor implementation to > > gwTerm::gwTerm( char const *text ) > : ref(PL_new_term_ref()) > { > if( !PL_chars_to_term( text, ref ) ) > throw RC_Syntaxfehler; > } .