C++ class constructor
Hi,
when I create a class and it''s constructor fails, how to handle it, that this class gets NULL?
throw an exception. . thats how to do it... you cant return a value because constructors must not have a return .
or you could set a value indicating the object failed to initialiase..
{ Stating the obvious never helped any situation !! }
or you could set a value indicating the object failed to initialiase..
{ Stating the obvious never helped any situation !! }
This is precisely why so many wrappers over C libraries have an "initialize()" type of call, as opposed to just doing it in the constructor. They want to return a value if something failed to initialize properly.
Exceptions are cleaner, and safer.. but if your adverse to using them (such as programming in a low memory/slow processor environment) the construction through a member method is probably the most commonly used.
I can give you many many reasons as to why this is bad, but just know that it is used (in MFC for example).
Exceptions are cleaner, and safer.. but if your adverse to using them (such as programming in a low memory/slow processor environment) the construction through a member method is probably the most commonly used.
I can give you many many reasons as to why this is bad, but just know that it is used (in MFC for example).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement