MyClass* const Func(const std::string& statename)
Run Code Online (Sandbox Code Playgroud)
因为这个封面是错误的
返回类型的解析警告(PW.USELESS_TYPE_QUALIFIER_ON_RETURN_TYPE)类型限定符无意义.
我们真的需要在这里删除const吗?
c++ static-analysis coding-style coverity-prevent function-signature
我在一本书中读到过tap函数(也称为K-Combinator)的函数签名如下:
tap :: (a -> *) -> a -> a
Run Code Online (Sandbox Code Playgroud)
“该函数接受一个输入对象 a 和一个对 a 执行某些操作的函数。它使用提供的对象运行给定的函数,然后返回该对象。”
const tap = fn => a => { fn(a); return a; };
tap((it) => console.log(it))(10); //10
Run Code Online (Sandbox Code Playgroud)
const tap = a => fn => { fn(a); return a; };
tap(10)((it) => console.log(it)); //10
Run Code Online (Sandbox Code Playgroud)
const tap = (a, fn) => {fn(a); return a; };
tap(10, (it) => console.log(it)); //10
Run Code Online (Sandbox Code Playgroud) javascript functional-programming function-signature k-combinator
我有一个派生类,我希望其中一个函数覆盖其在基类中的版本,但具有不同的签名。简单的例子:
#include "stdio.h"
bool use_foo = false;
class Foo {
public:
virtual int func(double x) { printf ("%f in Foo!\n", x); }
};
class Bar : public Foo {
public:
int func(short x) { printf ("%d in Bar!\n", x); }
};
int main () {
Foo* A;
if (use_foo)
A = new Foo;
else
A = new Bar;
A->func(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使 A 被分配为派生类,上面的代码也会调用基类副本:
> g++ test.cpp -o test -O3 && ./test
2.000000 in Foo!
Run Code Online (Sandbox Code Playgroud)
因为(据我的理解)参数可以转换为匹配基类签名,并且派生类不会因为这种差异而覆盖它(但在这种情况下它不会隐藏它吗?)。如果我将基类函数更改short …
我有一个从对象获取属性的函数。
// Utils.ts
export function getProperty<T, K extends keyof T>(obj: T, key: string): T[K] {
if (key in obj) { return obj[key as K]; }
throw new Error(`Invalid object member "${key}"`);
}
Run Code Online (Sandbox Code Playgroud)
我想检查返回的属性是否是具有给定签名的函数,然后使用提供的参数调用该属性。
getProperty()用于动态获取对象的方法之一并调用它。我试过:
let property: this[keyof this] = utils.getProperty(this, name);
if (typeof property === 'function') ) { property(conf); }
Run Code Online (Sandbox Code Playgroud)
但这给出了“无法调用类型缺少调用签名的表达式。类型'any'没有兼容的调用签名。” 错误。我知道来自的属性getProperty()确实可以是任何类型,但如何确定它是带有(conf: {}): void签名的函数?
很多时候,我编写 C++ 函数时,似乎const比其他人使用了更多的修饰符。
例如,我编写 Excel .xlsx 文件,并为此使用 LibXL 库,并且文档提到了这样的函数:
bool writeNum(int row, int col, double value, Format* format = 0)
Run Code Online (Sandbox Code Playgroud)
我碰巧继承了这个函数,我的签名如下:
bool XLAPIENTRY writeNum(const int row, const int col, const double value, IFormatT<wchar_t> * format = 0);
Run Code Online (Sandbox Code Playgroud)
请注意const int行和列的 s。
const问题:省略s安全吗int?(很明显,对于指针来说,忽略指针的 const 是危险的,但是做一些 an 的本地副本int并不是那么危险)。请注意,包含 writeNum 函数的原始标头int也没有提到 const-ing s。为什么这里const省略了修饰符?
可能重复:
在C++中是否重载了main()?
这是我的代码:
#include <iostream>
int main(void* a, void* b)
{
std::cout << "hello standalone " << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
名称重整后,不同的参数应该有不同的符号名称(void* a, void* b)应该不同(int, char**),但是这个程序在运行时没有任何问题.
这是为什么?
在我的程序的主文件中,我有以下声明
int main()
{
Customer c;
Part p;
Builder b;
auto partsVec = readpartFile();
auto customerVec = readcustomerFile();
auto builderVec = readbuilderFile();
fexists("Parts.txt");
complexity(c, partsVec);
robotComplexity(partsVec,customerVec);
writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));
}
Run Code Online (Sandbox Code Playgroud)
我的头文件由以下内容组成
vector<Part> readpartFile();
vector<Customer> readcustomerFile();
vector<Builder> readbuilderFile();
float complexity(const Customer& c, const std::vector<Part>& parts);
void robotComplexity(vector<Part> vecB, vector<Customer> vecC);
double variability(const vector<Customer>& customerList, const vector<Builder>& builderList);
vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
void writeFile(vector<double> build);
Run Code Online (Sandbox Code Playgroud)
除了 robotsComplexity 之外,所有功能都链接在一起。我在 main 中声明此函数会产生以下错误。
重载函数“robotComplexity”的多个实例与参数列表匹配: -- function "robotComplexity(const std::vector> &parts, const …
c++ linker-errors header-files forward-declaration function-signature
c++ ×5
coding-style ×1
constants ×1
header-files ×1
javascript ×1
k-combinator ×1
overriding ×1
typechecking ×1
typescript ×1