Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright 2015 Natanael Russo <natanaelrusso18@hotmail.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- **/
- #include <iostream>
- using namespace std;
- int Sum( int n1, int n2 )
- {
- int r = n1 + n2;
- return r;
- }
- int main()
- {
- // A function pointer called pSum1 that receives two int parameters and returns an int
- int (*pSum1)(int,int);
- pSum1 = Sum;
- cout<<"1. Use the function pointer like if was a normal function: "<<pSum1(3,4)<<endl<<endl;
- cout<<"2. Use the function pointer like if was a function pointer: "<<(*pSum1)(4,4)<<endl<<endl;
- // A type of a function pointer
- typedef int (*PSum2)(int,int);
- PSum2 pSum2;
- pSum2 = Sum;
- // This is the same like the other example but the difference is the pointer function type
- cout<<"1. Use the function pointer like if was a normal function: "<<pSum2(3,4)<<endl<<endl;
- cout<<"2. Use the function pointer like if was a function pointer: "<<(*pSum2)(4,4)<<endl<<endl;
- // In this case I make a type definition
- typedef int Sum3(int,int);
- Sum3* pSum3;
- pSum3 = Sum;
- cout<<"1. Use the function pointer like if was a normal function: "<<pSum3(3,4)<<endl<<endl;
- cout<<"2. Use the function pointer like if was a function pointer: "<<(*pSum3)(4,4)<<endl<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement