A quick question about C++ functions


#1

For example:
draw_player( ( timer_border == 1 ? disp_up : disp_down) )
{
blah blah blah();
}

My question:
What do the " ? " and " : " mean? It’s quite abvoious that I’m new to C. I’m not trying to get help with homework or anything like that: I was just reading a tutorial and the author did’nt go into detail concerning the aftermention characters within that function. Please, any help would be greatly appreciated.

EDIT:
You know, after some thinking… does it mean some type of exception/catch thing?


#2

they simply are conditional operators, usually they are alternatives to if statments when all you need is a quick and compact binary switch.

you usually check for a result with an expression before the “?”, and then depending on the result it will execute one of the expressions on either side of the “:”

in the case you quoted the function performs a simple check, if timer_border equals 1 then it will call disp_up and then execute, if time_border equals any other value it will call disp_down

by the look of it I’d say that function is supposed to initialize a player of some kind, it first checks for something to determine if the display routines are up or not and then calls another function accordingly.

as I said the thing itself is a simple check and binary switch, the way it’s used in this particular case looks like a primitive error check.


#3

As ThE_JacO said the " ? : " operator is a replacement for if statement. For example:


       int i;
       if(some_variable == 1)
       {
    	i = some_other_variable1;
       }
       else
       {
    	i = some_other_variable2;
       }
  
   is basically the same thing as
   
   int i = some_variable == 1 ? some_other_variable1 : some_other_variable2;
   
   The " ? : " operators are quite handy in things like function parametrization:
     void draw_player(int blahblah )
         {
  	blah blah blah();
         }
       
       void some_initializer(int timer_border)
       {
  	// Calling blah blah with if else
  	int d;
  	if(timer_border == 1)
  	{
  		d = disp_up;
  	}
  	else
  	{
  		d = disp_down;
  	}
  	draw_player(d);
       
  	// Calling blah blah with ? :
  	draw_player(( timer_border == 1 ? disp_up : disp_down));
       }
               -------------------
               pipari
               -------------------

#4

If you’re new to C/C++ an excellent online resource (for C++ at least) is:

http://www.cplusplus.com

They have an online tutorial and much of the content they present is generally applicable to C (when not, the tutorial makes it explicitly clear).

Dave


#5

For general reference, I use http://www.cplusplus.com/ref/ for IOStream reference, and http://www.cppreference.com/ for everything else…

http://www.cprogramming.com/ also has some good stuff on…


#6

One of the best C++ sites on the planet is http://www.moderncppdesign.com/ Be warned, and I say this with all due humility, there is some very powerful and advanced stuff on there that can be hard to get to grips with at first. I would strongly urge any C++ developer to stick with it though - it’s changed my C++ programming dramatically over the last year (and I’ve been doing this for 12 years and don’t consider myself a noob).

C++ templates as a Turing Complete functional langauge anyone? :slight_smile: If you think you know C++ take a look at Andrei Alexandrescu’s stuff. His site is a great hub site and links to just about anyone else worth your time learning from.


#7

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.