这怎么可能在 Django 中创建重载视图?问题是我有一个观点,它可以只接受一个参数,也可以不接受。如果有参数,它将传递给视图。
让我们展示我正在尝试做的事情
视图.py
def member_list(request,message):
memberList = Member.objects.all()
return render_response(request, 'user/member_list.html', {'memberList': memberList, 'message':message })
def member_list(request,message):
memberList = Member.objects.all()
return render_response(request, 'user/member_list.html', {'memberList': memberList, 'message':message })
Run Code Online (Sandbox Code Playgroud)
网址.py
(r'^member/list/$', 'views.member_list'),
(r'^member/list/(?P<message>[-\w]+)/$', 'views.member_list'),
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
谢谢
根据Linux 手册页,Linux C APIopen有两个原型如下:
int open(const char *pathname, int oflags);
int open(const char *pathname, int oflags, mode_t mode);
Run Code Online (Sandbox Code Playgroud)
让我困惑的是:
为什么 Linux C API 'open' 支持函数重载??
以下尝试运算符重载时发生错误:
#include<iostream>
#include<string>
#include<ostream>
using namespace std;
class Dollar
{
private:
float currency, mktrate, offrate;
public:
Dollar(float);
float getDollar() const;
float getMarketSoums() const;
float getofficialSoums() const;
void getRates();
// In the following function I was trying to overload "<<" in order to print all the data members:
friend void operator<<(Dollar &dol, ostream &out)
{
out << dol.getDollar() << endl;
out << dol.getMarketSoums() << endl;
out << dol.getofficialSoums() << endl;
}
};
Dollar::Dollar(float d)
{
currency = d;
}
float Dollar::getDollar() …Run Code Online (Sandbox Code Playgroud) 我一直在学习委托(在 C# 中),我读过的所有文章都说委托不能重载(与方法不同)。但是当我使用 'Func' 和 'Action' 方法(它们是 .NET 内置委托)时,我注意到它们已被重载:
这怎么可能?
我有一个重载函数,它们的定义是相同的,只有一个参数改变。所以,我试图创建一个模板函数,但是当我调用它时,我得到了一个符号查找错误。
头文件
void func(Myobject obj);
void func(MySecondObject obj);
Run Code Online (Sandbox Code Playgroud)
源文件
template<typename T>
void func(T obj) { obj.foo(); }
Run Code Online (Sandbox Code Playgroud)
测试文件
#include "MyHeaderFile.h"
func(MySecondObject()); // symbol lookup error at runtime
Run Code Online (Sandbox Code Playgroud)
谢谢。
大家好我写了两个代码
1.
#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int a = 10, b = 20;
cout << "value of a before swap " << a << endl;
cout << "value of b before swap " << b << endl;
swap(&a, &b);
cout << "value of a after swap " << a << endl;
cout << "value of b after swap " …Run Code Online (Sandbox Code Playgroud) 考虑这个程序-
#include <string>
#include <vector>
#include <set>
void fun(const std::string& val) {
}
void fun(std::vector<std::string> val) {
}
int main()
{
std::set<std::string> example;
fun({std::begin(example), std::end(example)});
}
Run Code Online (Sandbox Code Playgroud)
在编译时,我遇到了这些错误-
prog.cc: In function 'int main()':
prog.cc:13:49: error: call of overloaded 'fun(<brace-enclosed initializer list>)' is ambiguous
13 | fun({std::begin(example), std::end(example)});
| ^
prog.cc:4:6: note: candidate: 'void fun(const string&)'
4 | void fun(const std::string& val) {
| ^~~
prog.cc:7:6: note: candidate: 'void fun(std::vector<std::__cxx11::basic_string<char> >)'
7 | void fun(std::vector<std::string> val) {
| ^~~
Run Code Online (Sandbox Code Playgroud)
我知道它std::string有一个构造函数重载,它接受 …
考虑以下代码片段:
enum class Bar {
A
};
void foo(Bar) {}
struct Baz {
void foo() {
foo(Bar::A);
}
};
Run Code Online (Sandbox Code Playgroud)
它无法编译,来自 gcc 9.2 的消息是:
:12:19: error: no matching function for call to 'Baz::foo(Bar)'
12 | foo(Bar::A);
|
Run Code Online (Sandbox Code Playgroud)
我不怀疑这是一个错误,因为 clang 10 也失败了。关于这种情况,我有两个问题:
标准在哪里定义了这种重载的行为?
以这种方式指定编译器行为的可能原因是什么?
在Java中,当方法具有相同名称时,我们可以进行重载。我在谷歌上搜索,它说 C 不支持重载,所以我想知道是否有一种方法可以做类似于 C 中的重载的事情。
例如:
int max(int num1, int num2);
double max(double num1, num2);
int main(){
printf(...);
printf(...);
}
int max(int num1, int num2){
...
}
double max(double num1, double num2){
...
}
Run Code Online (Sandbox Code Playgroud) 是否有一个 Common Lisp 构造是 to labelswhat defmethodis to defun?也就是说,我想使用labels(或类似的东西)来定义几个具有相同名称但它们接受的参数不同的局部函数,并让编译器在其中进行选择。
作为 MWE,我想实现以下功能
(defmethod write-first-item-type ((lst list))
"Writes the type of the first item in lst."
(labels ((write-single ()
(format t "a single float"))
(write-double ()
(format t "a double float")))
(format t "The first item is: ~A.~%"
(cond ((eql (type-of (car lst)) 'single-float)
(write-single))
((eql (type-of (car lst)) 'double-float)
(write-double))
(t
(format t "unknown"))))))
Run Code Online (Sandbox Code Playgroud)
像
(defmethod write-first-item-type ((lst list))
"Should write the type of the first …Run Code Online (Sandbox Code Playgroud) overloading ×10
c++ ×5
c ×2
name-lookup ×2
templates ×2
.net ×1
api ×1
c# ×1
common-lisp ×1
delegates ×1
django ×1
django-views ×1
function ×1
iterator ×1
linux ×1
ostream ×1
parameters ×1
swap ×1
types ×1