4 Replies - 3172 Views - Last Post: 13 February 2010 - 06:17 AM Rate Topic: -----

#1 Guest_mon mon*


Reputation:

Function Templates and Unary Scope Resolution Operator

Posted 12 February 2010 - 02:06 PM

please i don't understand (what means Function Templates and Unary Scope Resolution Operator)
in this file in attach and give example
Is This A Good Question/Topic? 0

Replies To: Function Templates and Unary Scope Resolution Operator

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Function Templates and Unary Scope Resolution Operator

Posted 12 February 2010 - 04:38 PM

Templates are a method in C++ of using an unknown type in a function.
template <class T>
T _max(T a, T B)/> {
    return a>b?a:b;
}
You can call it as follows:
int a = 2, b = 4;
std::cout << _max<int>(a, B)/> << std::endl;
//Or...
float c = 2.3f, d = 6.77f;
std::cout << _max<float>(c, d) << std::endl;
//Or even...
std::stack<int> e, f;
e.push(20);
f.push(33);
std::cout << _max<std::stack<int>>(e, f).top() << std::endl;

Also, read C++ Scope Resolution Operator.

This post has been edited by PlasticineGuy: 12 February 2010 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 428
  • Joined: 28-September 08

Re: Function Templates and Unary Scope Resolution Operator

Posted 12 February 2010 - 05:11 PM

Templates allow you to define a common data type.
For example

template<class T>
void swap(T& val_1, T& val_2)
{
   T temp;
   temp = val_1;
   val_1 = val_2;
   val_2 = temp;
}



So the above snippet applies the address-of (unary) operator which is a pointer
to the variable, meaning changes made to either val_1 and val_2 are commited.
The function is a template function meaning I can pass any value in and it will
perform the swap, as long as the assignment (unary) operator is defined for that
data type.

So above I can use the template function to swap string values. (Note: Use *.equals(*) in Java.)
swap(stringA, stringB);



Look up operator overloading for more info on unary operators.
Also the template methodology can be implemented not only in functions
but classes as well.
template<class T>
class someClass
{
   // Use T any where in here.
   // ...
}


Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Function Templates and Unary Scope Resolution Operator

Posted 13 February 2010 - 06:13 AM

View Posttrixt.er, on 13 February 2010 - 12:11 AM, said:

Templates allow you to define a common data type.
I'm not sure if that's how i'd word it. Templates allow code to be generated at compile-time by allowing a programmer to write incomplete 'generic' code, then later using a substitution technique to fill in missing information - That information doesn't necessarily need to be a data type, it can be a constant integral expression too - e.g. enums, int, bool, etc. Conceptually its a bit like writing a type-safe preprocessor macro.


View Posttrixt.er, on 13 February 2010 - 12:11 AM, said:

template<class T>
void swap(T& val_1, T& val_2)
{
   T temp;
   temp = val_1;
   val_1 = val_2;
   val_2 = temp;
}



So the above snippet applies the address-of (unary) operator which is a pointer
to the variable, meaning changes made to either val_1 and val_2 are commited.
No, that & is not a unary operator meaning 'address of' - its a type qualifier which declares an identifier as a reference (an alias) for another object/variable elsewhere in the program.


View Posttrixt.er, on 13 February 2010 - 12:11 AM, said:

The function is a template function meaning I can pass any value in and it will
perform the swap, as long as the assignment (unary) operator is defined for that
data type.
A unary operator takes only one operand, I assume you meant 'binary'.

This post has been edited by Bench: 13 February 2010 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Function Templates and Unary Scope Resolution Operator

Posted 13 February 2010 - 06:17 AM

What indeed does the unary scope operator have to do with operator overloading? You can't overload ::.

The OP also asked a question about templates.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1