Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - Constructor bug or me?
Don't know if this is a compiler bug or me doing something undefined. Had a string class with a size member and a minimum allocation member. Default constructor originally like this: string() : min(64),sz(min),ln(0) { p=new char[sz+1]; *p=0; } It worked fine until a certain amount (quite a lot) of memory had been consumed by the program then started crashing as 'p' was null after the new statement. Changed to: string() : min(64),sz(64),ln(0) { p=new char[sz+1]; *p=0; } and worked fine. Can you not use previous member constructor values in the constructor list then? Just curious really. Paul Sep 15 2005
You can use them, just make sure that they're listed in the order you want them initialized: class string { string() : min(64) , sz(min) , ln(0) { //... } private: int min; // These must appear in this order int sz; // if sz comes first, min has an undefined value int ln; // when used in sz's initialization }; HTH Pablo "Paul" <Paul_member pathlink.com> wrote in message news:dgbund$176c$1 digitaldaemon.com...Don't know if this is a compiler bug or me doing something undefined. Had a string class with a size member and a minimum allocation member. Default constructor originally like this: string() : min(64),sz(min),ln(0) { p=new char[sz+1]; *p=0; } It worked fine until a certain amount (quite a lot) of memory had been consumed by the program then started crashing as 'p' was null after the new statement. Changed to: string() : min(64),sz(64),ln(0) { p=new char[sz+1]; *p=0; } and worked fine. Can you not use previous member constructor values in the constructor list then? Just curious really. Paul Sep 15 2005
Yep. That explains it. Thanks. Paul In article <dgcbp8$1mll$1 digitaldaemon.com>, Pablo Aguilar says...You can use them, just make sure that they're listed in the order you want them initialized: class string { string() : min(64) , sz(min) , ln(0) { //... } private: int min; // These must appear in this order int sz; // if sz comes first, min has an undefined value int ln; // when used in sz's initialization }; HTH Pablo "Paul" <Paul_member pathlink.com> wrote in message news:dgbund$176c$1 digitaldaemon.com...Don't know if this is a compiler bug or me doing something undefined. Had a string class with a size member and a minimum allocation member. Default constructor originally like this: string() : min(64),sz(min),ln(0) { p=new char[sz+1]; *p=0; } It worked fine until a certain amount (quite a lot) of memory had been consumed by the program then started crashing as 'p' was null after the new statement. Changed to: string() : min(64),sz(64),ln(0) { p=new char[sz+1]; *p=0; } and worked fine. Can you not use previous member constructor values in the constructor list then? Just curious really. Paul Sep 20 2005
|