Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ default function

Options
  • 25-03-2003 2:43pm
    #1
    Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,523 Mod ✭✭✭✭


    I have something like the following...it recognises 8 prototypes depending on the input....my problem is that I want to put in a default function which will be exected when the input does not trigger any of the 8 functions....I've tried a few different approaches but they all seem to go around in circles.


    //loop
    {
    function1();
    funtion2();
    function3();
    funtion4();
    function5();
    funtion6();
    function7();
    funtion8();

    default function(); /*try to catch things which don't trigger on of the other 8*/

    }
    //end loop


Comments

  • Registered Users Posts: 6,240 ✭✭✭hussey


    I might be missing something but could you try a switch statement
    or if-else

    if (cond1/prototype1)
    function 1
    else if (cond2/prototype2)
    function2

    ....

    else
    defaultfunction()

    ?????


  • Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,523 Mod ✭✭✭✭BossArky


    yes, I know that would work alright if I restructured my program...but I was wondering if there is a way to trigger the default function if none of the others are triggered in the way I have shown in the first post.

    I had the default function full of

    if( != function1 && != function2 ...etc)
    {
    //blah
    }


    it should work in theory ..but didn't.

    Is there another way...maybe by setting a flag or using pointers?


  • Registered Users Posts: 1,931 ✭✭✭Zab


    I think that you are missing something here: in your code all the functions are always executed. I presume that they contain logic that will either do something or not, and return a boolean value saying whether they did it or not. But the point is that each of the functions are executed, whether they do anything or not.

    If you really don't want to change the structure of your code, you could try something like this.
    bool bRan = false;
    bRan |= function1();
    bRan |= function2();
    ...
    if ( !bRan )
      defaultFunction();
    

    This, of course, presumes that you return true if the function meets its condition, and false if it does't. However, you may want to reevaluate how you are structuring your program.

    Zab.


  • Registered Users Posts: 6,240 ✭✭✭hussey


    I'm a bit confused on what you want to do exactly

    would I right in saying :

    that if it doesn't go into function1 or 2 or 3 etc

    it should go into the default?

    if so what you could do is this

    flag = false

    call function1
    function1 :
    flag = false
    check input value
    if ok flag = true

    return flag
    if flag = false then go default function

    this way the only way flag could be true is if goes into func1 and everything is ok


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    Which would be more efficient? To have a global boolean variable that's just changed from within the function if its called or to have a boolean variable local to the loop which is handed to the function if its called? I can't remember exactly but isn't there a way to check if a variable is of a certain type in c++? If it is I'd suggest you use a switch statement, going to be a lot clearer for someone to read afterwards. If you're performing the same task 8 times and just changing each function to handle different variable types you'd be better off just using a class with templates.


  • Advertisement
  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    BossArky:

    You are calling all functions in your sample code. You need to rephrase your questions. My guess is the answer you are looking for is a switch or a virtual function (in the latter case the class with that function would be what determines what is called).

    Thorbar:

    Globals are generally less efficient than locals, and almost always more error-prone. You don't normally need to check what type a variable is of in C++ because it's a staticly-typed language, so that is determined at compile-time. You do sometimes need to determine what type of object is pointed to by a pointer or reference to a class that might be subclassed, but even then you normally don't need to determine that.


  • Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,523 Mod ✭✭✭✭BossArky


    Thanks all for your suggestions, I restructured my program as a heap of if-else statements and it works fine now.

    Think I had a bug somewhere in the first version....and I suppose it is alot more efficient not to call everything at the one time.

    Thanks again.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    Well lets say you had an if statement that if it was run used type casting to convert a number to a string. Is there anyway to test if that variable was a number or a string without just repeating the if statement condition test?


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by Thorbar
    Well lets say you had an if statement that if it was run used type casting to convert a number to a string. Is there anyway to test if that variable was a number or a string without just repeating the if statement condition test?

    You can't cast a number to a string in C++ (well you can change a number into a char* or wchar_t* so that the number 0x234393F0 becomes the string of chars or wchar_ts starting with the char or wchar_t at address 0x234393F0, but that's rarely a good idea).

    To get a string out of a number you'd have to do something like call itoa or put the number into a basic_ostringstream.

    Conversely trying to cast "34" to int directly won't give you 34, but the address in memory of the character '3' that starts that string.

    A more likely case would be that you had a class B and D1 and D2 were publicly derived from it. In that case a B* or B& point or refer to a B, D1, D2 or some object unknown to the person writing the code that receives that pointer or reference.

    In that case you could use either dynamic_cast or RTTI to discover if it was a D1. Generally though you don't need to, you would just call functions, especially virtual functions on the object without caring what type of object was actually servicing them. If you find yourself frequently having to cast "down" then most likely something is very wrong with you class-hierarchy design.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    Well I've never actually used any of this in a program was just curious if c++ had a means to telling what type a variable was. I guess you could make a function of the same name for each type of variable in c++ with polymorphism(spelling?) have each one take in a different type as its parameter and just return a string with that as its name. But as you pointed out if you need something like this you're probably doing something wrong.


  • Advertisement
  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by Thorbar
    Well I've never actually used any of this in a program was just curious if c++ had a means to telling what type a variable was. I guess you could make a function of the same name for each type of variable in c++ with polymorphism(spelling?) have each one take in a different type as its parameter and just return a string with that as its name. But as you pointed out if you need something like this you're probably doing something wrong.

    Well in the case where you want to have different functions that do similar things with different variable types you can overload functions:
    int inline double(int x){
    	return x*2;
    }
    
    long inline double(long x){
    	return x*2;
    }
    

    I didn't mention this in my previous posts because this is worked out at compile time, I didn't think of it as C++ determining the variable time (which made me think or run-time resolution).

    It's worth noting that the above two functions could also be done as:
    template<class T>
    	T inline double(T x){
    	return x*2;
    }
    
    Which would produce the above functions as needed.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    Isn't polymorphism another work for overloading functions or am I just not remembering first year lectures correctly?


Advertisement