Allowing certain function call only if it is followed by another function

Tyler Gajewski

I want to create a function that must be called with { } braces. Similar to if, while and for statements. The reason for this is to prevent the operator from calling the function without adding the necessary code that follows.

#include <iostream>

/* function to run the test */
bool runTest()
{
    std::cout "PASS" << std::endl;
    return true;
}

/* function will print the passed chars */
void newTest(const char *functionName, const char *testName)
{
    std::cout << functionName << " " << testName;
}

int main(void)
{
    /* OPTION 1 */
    /* You can call it like this */
    newTest("runTest", "TestName");
        runTest();
    / * OR */
    newTest("runTest", "TestName");
    {
        runTest();
    }

    /* OPTION 2 */
    /* I want the operator to be required to do */
    newTest("runTest", "TestName")
    {
        runTest();
    }
    return 0;
}

I want option 2 to be the correct usage. Because with option 1 you could call that like:

newTest("runTest", "TestName");
newTest("otherTest", "TestName");
   runTest();

but you would not have the necessary call to runTest() that is related to the first calls output.

The output would be:

runTests TestName /*(NULL)*/
otherTest TestName PASS

This way of calling is just checking to make sure the operator called the test after the newTest() function.

Ideally if the operator was perfect he could call the functions properly.

newTest("runTest", "TestName");
    runTest();
newTest("otherTest", "TestName");
   runTest();

But i want to eliminate the error but automatically requiring the operator to call the function with {} following the call.

John Bargman

Firstly, those 'curly brackets' are called 'braces' - you may want to edit your question so people understand better.

unless someone changed it without telling me; C++ doesn't work this way. Braces in this context are reserved explicitly for the scope of keywords, function-definitions, and initializer lists.

void Badger()
{ //this is fine, it's a function-definition
}
int main()
{
for(I =0; I < 5; I++)
   { //this is fine, for is a keyword.
   }
foo()
   { //This is not legal, foo must take it's perimeters within brackets, not braces.
   } 
}

If you wish a user to pass code to a function, you must do so using a Function-pointer, the user then may pass a existing function, or a lambda.

void Foo(bool (Function_ptr*) ()) { };
bool Function_To_Pass() { } ;
int main()
{
    Foo(&Function_To_Pass);
    Foo([]()
    {
        //Code to run here
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there any way to call a function by another function argument?

From Dev

Call a function after another function has completed

From Dev

Call another function with jQuery

From Dev

Only call function once?

From Dev

Pass a function into another function and call it

From Dev

How to call setinterval function in another function is not working

From Dev

call a function after another function is completed

From Dev

Call function from another function Mysql

From Dev

How to call a function within another function in javascript

From Dev

C function call followed by a comma separator

From Dev

How to call a function in another function in protractor

From Dev

how to call a certain function

From Dev

How do I call a function only if another function is complete? (Swift and retrieving from Parse)

From Dev

Swift, call generic function in another generic function

From Dev

Swift Generic function call another Generic function

From Dev

Call a __init__ function from another function

From Dev

apply() function to only certain columns

From Dev

how to call a function from another function in Jquery

From Dev

Is there any way to call a function by another function argument?

From Dev

Call function from another function in jQuery OOP

From Dev

ActionScript call a function within another function

From Dev

Only call function once?

From Dev

How to call a number from another function into a function?

From Dev

Javascript function call with another function as parameter

From Dev

How call a function inside another function

From Dev

Allowing a function to see listbox in another form

From Dev

Only allowing certain characters

From Dev

How to call a function within another function in javascript

From Dev

Call WP plugin function only if certain condition is met

Related Related

  1. 1

    Is there any way to call a function by another function argument?

  2. 2

    Call a function after another function has completed

  3. 3

    Call another function with jQuery

  4. 4

    Only call function once?

  5. 5

    Pass a function into another function and call it

  6. 6

    How to call setinterval function in another function is not working

  7. 7

    call a function after another function is completed

  8. 8

    Call function from another function Mysql

  9. 9

    How to call a function within another function in javascript

  10. 10

    C function call followed by a comma separator

  11. 11

    How to call a function in another function in protractor

  12. 12

    how to call a certain function

  13. 13

    How do I call a function only if another function is complete? (Swift and retrieving from Parse)

  14. 14

    Swift, call generic function in another generic function

  15. 15

    Swift Generic function call another Generic function

  16. 16

    Call a __init__ function from another function

  17. 17

    apply() function to only certain columns

  18. 18

    how to call a function from another function in Jquery

  19. 19

    Is there any way to call a function by another function argument?

  20. 20

    Call function from another function in jQuery OOP

  21. 21

    ActionScript call a function within another function

  22. 22

    Only call function once?

  23. 23

    How to call a number from another function into a function?

  24. 24

    Javascript function call with another function as parameter

  25. 25

    How call a function inside another function

  26. 26

    Allowing a function to see listbox in another form

  27. 27

    Only allowing certain characters

  28. 28

    How to call a function within another function in javascript

  29. 29

    Call WP plugin function only if certain condition is met

HotTag

Archive