sukumar
sukumar

Reputation: 161

checking of constructor parameter

class item
{
    int i;

  public:
    item(int no) {

    }
};

I want to check the constructor parameter. If it is found to hold a negative value, then object creation should be stopped.

Exceptions can not be used here as the targeted system does not support exceptions.

Upvotes: 6

Views: 6572

Answers (6)

Matteo Italia
Matteo Italia

Reputation: 126827

Exceptions are the way designated by the standard to perform this task; there's no other way to abort completely the object creation.

On the other hand, you can give your class some "state" member that specifies that the class was not correctly constructed and check it at every method call (a bit like how iostream classes work).

class item
{
    int i;
    bool validState;
public:
    item(int no) : validState(true)
    {
        if(/* no is invalid */)
        {
            validState = false;
            return;
        }
        /* ... */
    }

    bool ValidState() { return validState; }

    SomeType DoSomething()
    {
        if(!ValidState())
        {
            // do nothing/report the error to the caller
        }
        // ...
    }
}

IMO it's cumbersome, but if you don't have exceptions and want to create objects via a public constructor there's nothing much better than this.

Upvotes: 3

Brian Fearon
Brian Fearon

Reputation: 151

You cannot stop object construction mid-way, without throwing an exception. That said, you may outright prevent construction of item objects which fail a precondition, by moving the precondition and object-creation responsibilities to a separate factory function, and making the constructors private (to disallow all other ways to construct the object):

class item {
       int i;

   public:
       static item* create( int no )
       { 
           return no < 0 ? NULL : new item( no );
       }

   private:
       item() { ... }
       item( int no ) { ... }
};

Upvotes: 2

Jon
Jon

Reputation: 437414

There is no way to stop the creation of an object without throwing. The best you can do is set an "invalid parameter" flag that you have to check afterwards, and if true discard the object without using it.

With the requirements you have, it would probably be better to use a factory method to create the objects -- this way, you can make the checks before calling the constructor:

class item
{
    int i;
public:
    static item* create(int no) {
        if (no < 0) {
            return 0;
        }

        return new item(no);
    }

private:
    item(int no) {

    }
};

You could use this like

item* myItem = item::create(-5);
if(!myItem) {
    // failed
}

However, this forces you to allocate all item instances on the heap.

Upvotes: 6

Ed Heal
Ed Heal

Reputation: 60007

Put the object into an error state (use a boolean) and then all methods should return an error.

ie

class Item
{
   int i;
   bool errorState;
   public:
      Item(int n) : i(n) {
         errorState = i < 0;
      }
      bool method()
      {
        if (errorState) return false;
         ..... do stuff here

         return true;
      }
}

Upvotes: 0

Nim
Nim

Reputation: 33655

Three options.

  1. Use a flag in your class to track full construction - but you'll have to test that in each method.
  2. Make item a wrapper, such that the internals are held in a class that is constructed if the arguments are good, but in all the methods, you'll have to test the internals - so no different to 1 anyway.
  3. Use a factory to return a smart pointer if the arguments are good.

My preference in this scenario is the last one.

Upvotes: 1

iammilind
iammilind

Reputation: 69998

You can do it at compile time with necessary warning flags ON (e.g. -Wall in gcc).

class item
{
public:
    item(unsigned int no) {}  // item takes only +ve value
};

Compiler will warn you if a -ve value is passed.

Upvotes: -1

Related Questions