Class execution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Riaan

    #1

    Class execution

    Hi Everybody,

    How do I stop my class of being used after certain init functions failed in
    my constructor ?

    Be Good
    --
    Riaan Bekker


  • Ian Collins

    #2
    Re: Class execution

    Riaan wrote:[color=blue]
    > Hi Everybody,
    >
    > How do I stop my class of being used after certain init functions failed in
    > my constructor ?
    >[/color]
    Call abort or use an assert. There is no other generic way.
    [color=blue]
    > Be Good[/color]

    I'll try :)

    --
    Ian Collins.

    Comment

    • improgrammer

      #3
      Re: Class execution

      The generic solution is to throw an exception.
      "use an assertion" is one form of throw an exception.

      Comment

      • Tomás

        #4
        Re: Class execution

        Riaan posted:
        [color=blue]
        > Hi Everybody,
        >
        > How do I stop my class of being used after certain init functions
        > failed in my constructor ?
        >
        > Be Good[/color]



        class Monkey {
        public:
        class InitError {}; // Just a dummy type

        Monkey(int a)
        {
        if ( !a )
        {
        throw InitError();
        }
        }
        };

        int main()
        {
        try {
        Monkey ape;
        }
        catch( InitError const k )
        {

        }
        }

        Unchecked code, may contain errors/bugs.

        -Tomás

        Comment

        • Tomás

          #5
          Re: Class execution


          [color=blue]
          > catch( InitError const k )[/color]


          catch ( Monkey::InitErr or const k)

          Comment

          • Ben Pope

            #6
            Re: Class execution

            Tomás wrote:[color=blue][color=green]
            >> catch( InitError const k )[/color]
            >
            >
            > catch ( Monkey::InitErr or const k)[/color]

            catch ( Monkey::InitErr or const& k)

            Ben Pope
            --
            I'm not just a number. To many, I'm known as a string...

            Comment

            • Rein Anders Apeland

              #7
              Re: Class execution

              improgrammer wrote:[color=blue]
              > The generic solution is to throw an exception.
              > "use an assertion" is one form of throw an exception.
              >[/color]

              AFAIK 'using an assertion' is NOT a form of 'throwing an exception'.
              Exceptions can be caught, assertions cannot. Assertions will abort your
              program if the fail.

              -Rein

              Comment

              Working...