Welcome, Guest. Please login or register.

Author Topic: question about C++ constructors  (Read 23004 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Treke

  • Newbie
  • *
  • Join Date: Feb 2002
  • Posts: 45
    • Show all replies
Re: question about C++ constructors
« on: January 24, 2003, 08:47:32 AM »
Or you can do something like that. (another proposal ) Ctor:

MayClass :: MyClass ()
                    : blValidityFlag ( true )
{
  try
      {
      //Allocate, instantiate stuff
       }
   catch (/*whatever you want to catch*/)
       {
       validityFlag = false;
       }
}

// public method, can be implemented also throwing
// exception
MyClass :: IsValid ()
  {
  return validityFlag;
  }

// then after constructor you can call a IsValid method
// if its not valid you can propagate and exception
// of coruse the best is to do a class which
// implements only Isvalid and derive your class from
// Something like IValidable interface ;)))

// So the usage can be

MyClass instance;
instance.Isvalid (); //can throw exception or return
                                   //false

There are, I guess (I am sure) more another posibilities. The goal is to be allways informed what failed, and and have the track of your "error source", "error flow" ....

re

Treke
 

Offline Treke

  • Newbie
  • *
  • Join Date: Feb 2002
  • Posts: 45
    • Show all replies
Re: question about C++ constructors
« Reply #1 on: January 24, 2003, 12:03:51 PM »
Quote
found that the method described in Bjaarne Stroustrup's 'The C++ Programming Language' 3rd Ed. works for me...


Probably the best book is Design patterns by Gamma, Helm,Johnson and vlissides. The issue (why, how, and so on..)is called creational patterns (ie Abstract factory, Factory method. prototype.... )

re
Treke