Thread: Re: [Algorithms] C++ inherited constructors
Brought to you by:
vexxed72
From: Adam M. <amo...@dp...> - 2000-07-15 19:14:33
|
That, together with the fact that constructors can't return error codes (only throw exceptions) when something goes wrong, has me following a very simple design heuristic ever since I can remember: Don't make a constructor do anything other than initializing member variables. In other words, don't let it do anything that can go wrong. Put that code (for example the thread creation code in your case) in another method that has to be called after construction. If you had done that when you designed the CThread class, you wouldn't have a problem. -- --Adam Moravanszky http://www.n.ethz.ch/student/adammo -----Original Message----- From: Matt Adams <de...@gm...> To: gda...@li... <gda...@li...> Date: Saturday, July 15, 2000 1:19 PM Subject: Re: [Algorithms] C++ inherited constructors >I need this for aesthetic reasons :) >I've got an abstract CThread class. If a want to create a new thread, I >build my process class and inherit CThread, which creates a new thread by >construction. >Now I want my main-threadloop-class to inherit CThread, too, to provide all >the thread functionality to my main thread. But that thread already exists > created by compiler ), and shouldn't be created again by CThread. >Of course I can put CThread () and ~CThread () in functions like Create() >and Destroy() and leave the constructor / destructor empty. But it'd be >nicer if it could be done in another way... > >----- Original Message ----- >From: Favnir >To: gda...@li... >Sent: Saturday, July 15, 2000 1:32 PM >Subject: Re: [Algorithms] C++ inherited constructors > >The only way you can do this is to define a (preferably) protected >do-nothing constructor for the base class, and inherit it in the subclass >constructor. > >But, why in the world would you need to do this, in the first place? > >Are, >F > >----- Original Message ----- >From: Matt Adams >To: gda...@li... >Sent: Saturday, July 15, 2000 12:22 PM >Subject: [Algorithms] C++ inherited constructors > > >Hi, > >I got a question about the C++ class hierarchy. > >class a >{ > a () {...}; // constr a >}; > >class b : a >{ > b () {...}; // constr b >}; > >b instance; > >When creating an instance of b, constructors for both class a and class b >are called. >Is there a way to suppress the automatic calling of constructor a ? >Or something like 'overloading' the old constructor by a new one ? >I couldn't find anything like that in the compiler docs. > >Any help appreciated, >Matt > > >_______________________________________________ >GDAlgorithms-list mailing list >GDA...@li... >http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > > >_______________________________________________ >GDAlgorithms-list mailing list >GDA...@li... >http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list |
From: Adam M. <amo...@dp...> - 2000-07-18 22:24:09
|
>You can always make the ctor throw an exception, which is an acceptable form >of error handling in this case. I did say exactly that in my message. Please read more carefully. -- --Adam Moravanszky >> That, together with the fact that constructors can't return error codes >> (only throw exceptions) when something goes wrong, has me following a very >> -- >> --Adam Moravanszky >> http://www.n.ethz.ch/student/adammo |
From: <ma...@ci...> - 2000-07-18 11:48:44
|
You can always make the ctor throw an exception, which is an acceptable form of error handling in this case. Matt, I think the solution to the problem is to add another constructor to your main-threadloop-class and your CThread which take an existing thread handle, e.g.: class MainThreadLoop { MainThreadLoop(HANDLE hThread) : CThread(hThread) /*, m_x(0)*/{/*etc*/} }; class CThread { CThread() { /* create a thread */ } CThread(HANDLE hThread) { /* use given thread */ } /* or: CThread(HANDLE hThread=0) { if (hThread==0) { hThread = CreateThread(...); } } */ }; WinMain(...) { MainThreadLoop MainLoop(GetCurrentThread()); } -=Mark=- Mark Atkinson, Technical Director, Computer Artworks Ltd. http://www.artworks.co.uk > That, together with the fact that constructors can't return error codes > (only throw exceptions) when something goes wrong, has me following a very > simple design heuristic ever since I can remember: Don't make a > constructor > do anything other than initializing member variables. In other words, > don't > let it do anything that can go wrong. Put that code (for example the > thread > creation code in your case) in another method that has to be called after > construction. If you had done that when you designed the CThread class, > you > wouldn't have a problem. > -- > --Adam Moravanszky > http://www.n.ethz.ch/student/adammo > > -----Original Message----- > From: Matt Adams <de...@gm...> > To: gda...@li... > <gda...@li...> > Date: Saturday, July 15, 2000 1:19 PM > Subject: Re: [Algorithms] C++ inherited constructors > > > >I need this for aesthetic reasons :) > >I've got an abstract CThread class. If a want to create a new thread, I > >build my process class and inherit CThread, which creates a new thread by > >construction. > >Now I want my main-threadloop-class to inherit CThread, too, to provide > all > >the thread functionality to my main thread. But that thread already > exists > > created by compiler ), and shouldn't be created again by CThread. > >Of course I can put CThread () and ~CThread () in functions like Create() > >and Destroy() and leave the constructor / destructor empty. But it'd be > >nicer if it could be done in another way... > > > >----- Original Message ----- > >From: Favnir > >To: gda...@li... > >Sent: Saturday, July 15, 2000 1:32 PM > >Subject: Re: [Algorithms] C++ inherited constructors > > > >The only way you can do this is to define a (preferably) protected > >do-nothing constructor for the base class, and inherit it in the subclass > >constructor. > > > >But, why in the world would you need to do this, in the first place? > > > >Are, > >F > > > >----- Original Message ----- > >From: Matt Adams > >To: gda...@li... > >Sent: Saturday, July 15, 2000 12:22 PM > >Subject: [Algorithms] C++ inherited constructors > > > > > >Hi, > > > >I got a question about the C++ class hierarchy. > > > >class a > >{ > > a () {...}; // constr a > >}; > > > >class b : a > >{ > > b () {...}; // constr b > >}; > > > >b instance; > > > >When creating an instance of b, constructors for both class a and class b > >are called. > >Is there a way to suppress the automatic calling of constructor a ? > >Or something like 'overloading' the old constructor by a new one ? > >I couldn't find anything like that in the compiler docs. > > > >Any help appreciated, > >Matt > > > > > >_______________________________________________ > >GDAlgorithms-list mailing list > >GDA...@li... > >http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > > > > > >_______________________________________________ > >GDAlgorithms-list mailing list > >GDA...@li... > >http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > > > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > > |
From: Matt A. <de...@gm...> - 2000-07-18 16:18:22
|
Yes, this is what I actually did. But I'm really thinking about the things Adam was saying maybe I should have a deeper look at me overall object structure. Currently I'm seeking some better OO-guidelines for a redesign - I want to avoid regularly ending up with problems due to a weak class design But of course Stephen Baker is right, and this list is not the right place to discuss it in all details ( Sorry, I didn't realize this'll be such a long thread ) Btw. thanks for the info about sweng-gamedev list, Scott Maybe that'll prevent some OT questions like mine in future;) Matt ----- Original Message ----- From: Mark Atkinson <ma...@ci...> To: <gda...@li...> Cc: <ma...@ci...> Sent: Tuesday, July 18, 2000 1:48 PM Subject: Re: [Algorithms] C++ inherited constructors > You can always make the ctor throw an exception, which is an acceptable form > of error handling in this case. > > Matt, I think the solution to the problem is to add another constructor to > your main-threadloop-class and your CThread which take an existing thread > handle, e.g.: > > class MainThreadLoop > { > MainThreadLoop(HANDLE hThread) : CThread(hThread) /*, m_x(0)*/{/*etc*/} > }; > > class CThread > { > CThread() { /* create a thread */ } > CThread(HANDLE hThread) { /* use given thread */ } > > /* or: CThread(HANDLE hThread=0) > { > if (hThread==0) { hThread = CreateThread(...); } > } */ > }; > > WinMain(...) > { > MainThreadLoop MainLoop(GetCurrentThread()); > } > > > -=Mark=- > > Mark Atkinson, Technical Director, Computer Artworks Ltd. > http://www.artworks.co.uk > > > > That, together with the fact that constructors can't return error codes > > (only throw exceptions) when something goes wrong, has me following a very > > simple design heuristic ever since I can remember: Don't make a > > constructor > > do anything other than initializing member variables. In other words, > > don't > > let it do anything that can go wrong. Put that code (for example the > > thread > > creation code in your case) in another method that has to be called after > > construction. If you had done that when you designed the CThread class, > > you > > wouldn't have a problem. > > -- > > --Adam Moravanszky > > http://www.n.ethz.ch/student/adammo > > > > -----Original Message----- > > From: Matt Adams <de...@gm...> > > To: gda...@li... > > <gda...@li...> > > Date: Saturday, July 15, 2000 1:19 PM > > Subject: Re: [Algorithms] C++ inherited constructors > > > > > > >I need this for aesthetic reasons :) > > >I've got an abstract CThread class. If a want to create a new thread, I > > >build my process class and inherit CThread, which creates a new thread by > > >construction. > > >Now I want my main-threadloop-class to inherit CThread, too, to provide > > all > > >the thread functionality to my main thread. But that thread already > > exists > > > created by compiler ), and shouldn't be created again by CThread. > > >Of course I can put CThread () and ~CThread () in functions like Create() > > >and Destroy() and leave the constructor / destructor empty. But it'd be > > >nicer if it could be done in another way... > > > > > >----- Original Message ----- > > >From: Favnir > > >To: gda...@li... > > >Sent: Saturday, July 15, 2000 1:32 PM > > >Subject: Re: [Algorithms] C++ inherited constructors > > > > > >The only way you can do this is to define a (preferably) protected > > >do-nothing constructor for the base class, and inherit it in the subclass > > >constructor. > > > > > >But, why in the world would you need to do this, in the first place? > > > > > >Are, > > >F > > > > > >----- Original Message ----- > > >From: Matt Adams > > >To: gda...@li... > > >Sent: Saturday, July 15, 2000 12:22 PM > > >Subject: [Algorithms] C++ inherited constructors > > > > > > > > >Hi, > > > > > >I got a question about the C++ class hierarchy. > > > > > >class a > > >{ > > > a () {...}; // constr a > > >}; > > > > > >class b : a > > >{ > > > b () {...}; // constr b > > >}; > > > > > >b instance; > > > > > >When creating an instance of b, constructors for both class a and class b > > >are called. > > >Is there a way to suppress the automatic calling of constructor a ? > > >Or something like 'overloading' the old constructor by a new one ? > > >I couldn't find anything like that in the compiler docs. > > > > > >Any help appreciated, > > >Matt > > > > > > > > >_______________________________________________ > > >GDAlgorithms-list mailing list > > >GDA...@li... > > >http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > > > > > > > > >_______________________________________________ > > >GDAlgorithms-list mailing list > > >GDA...@li... > > >http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > > > > > > _______________________________________________ > > GDAlgorithms-list mailing list > > GDA...@li... > > http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > > > > > > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > |