我的问题是如何翻译以下示例?这是一个返回int指针的函数吗?
<?php
if(isset($_GET['send'])){
$name= $_GET['name'];
$email= $_GET['email'];
$phone = $_GET['phone'];
$message = $_GET['message'];
if(isset($name) && isset($email) && isset($phone) && isset($message) && !empty($name) && !empty($email) && !empty($phone) && !empty($message)){
echo '<div class="alert alert-success" role="alert"><p class="alert">Your message was sent successfully.</p></div>';
$con = mysqli_connect('localhost', 'root', '') or die('Cannot connect to database.');
if(!mysqli_select_db($con, 'users')){
echo 'Database is not choosen.';
}
$sql = "INSERT INTO person(Name, Email, Phone, Message) VALUES ('$name', '$email', '$phone', '$message')";
if(!mysqli_query($con, $sql)){
echo "Data is not inserted.";
}
}
else{
echo '<div class="alert alert-danger" role="alert"><p class="alert">Some fields are empty.</p></div>';
}
}
?>
<form action = "contact.php" method="get">
<div class="form-group">
<input type="text" class="form-control" placeholder="Name" name="name">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
<div class="form-group">
<input type="number_format" class="form-control" placeholder="Phone" name="phone">
</div>
<div class="form-group">
<textarea class="form-control" rows="8" placeholder="Message" name="message"></textarea>
</div>
<div class="form-group">
<button class = "btn btn-primary" style="width: 100%; height: 70px" type="submit" name="send"><span class="glyphicon glyphicon-send"></span></button>
</div>
</form>
我可以编写使用它的程序吗? 提前谢谢!
答案 0 :(得分:1)
-com.apple.CoreData.ConcurrencyDebug 1
当然可以在程序中使用它。这不是太不寻常。我看到的声明要糟糕得多。
我重命名了你的&#34;功能&#34;到&#34; F&#34;为清楚起见。然后你可以写:
k
替代:
int* (*F)(int, int (*kFunc)(int *) );
答案 1 :(得分:0)
你应该删除额外的括号,这是正确的版本:
How to define pointer to pointer to function
解释(使用左右规则):
#include <iostream>
int bar(int*) {
std::cout << "inside bar\n";
return 0;
}
int* foo(int, int (*k)(int *)) {
std::cout << "inside foo\n";
k(nullptr);
return nullptr;
}
int main() {
int* (*function)(int, int (*k)(int *));
function = foo;
function(0, bar);
// Now, as you asked for, a pointer to pointer to above function
decltype(function) *pff;
pff = &function;
(*pff)(0, bar);
}
下面是一个关于如何使用它的简短示例,您也要求$ perl -MDateTime -wE 'say DateTime->today
->set_day(1)
->add(months => 1)
->subtract(days => 1)
->ymd'
,所以下面也包括这个。
http://coliru.stacked-crooked.com/a/d05200cf5f6397b8
2016-05-31
答案 2 :(得分:0)
有很多方法可以使用指向函数的指针,可能是像Factory这样的模式可以利用函数指针来创建新对象。(看这里:http://www.codeproject.com/Articles/3734/Different-ways-of-implementing-factories)
可能这段代码可以帮助你并提供有关功能指针的强大功能的想法。
#include <stdio.h>
#include <stdlib.h>
#include <map>
// Define the func ptrs
typedef void (*TFunc)(const char *, int);
typedef int (*TFunc2)(int);
int return_value(int i)
{
return i * 5;
}
void a( const char *name, int i )
{
printf ("a->%s %d\n\n", name, i);
}
void b( const char *name, int i)
{
printf ("b->%s %d\n\n", name, i);
}
struct test
{
const char *name;
int i;
TFunc func;
};
static test test_array[2] =
{
{ "a", 0, a },
{ "b", 1, b },
};
int main(int argc, char **argv, char** envp)
{
// Check the simple case, pointer to a function
TFunc fnc = a;
TFunc2 fnc2 = return_value;
fnc("blabla", 5);
fnc = b;
fnc("hello!", 55);
printf ("%d\n\n",fnc2(5));
//Check arrays of structs when there is a pointer to a fnc
test_array[0].func(test_array[0].name, test_array[0].i);
test_array[1].func(test_array[1].name, test_array[1].i);
//Handle a map of functions( This could be a little implementation of a factory )
typedef std::map<int, TFunc > myMap;
myMap lMap;
lMap.insert(std::make_pair(5, a));
lMap.insert(std::make_pair(2, b));
if( lMap.find( 5 ) != lMap.end() )
{
lMap[5]("hello map 5", 1);
}
myMap::iterator lItFind = lMap.find(2);
if( lItFind != lMap.end() )
{
lItFind->second("hello map 2", 2);
}
return(0);
}
我希望这会对你有所帮助。