TypeScript 新手在这里,我目前正在这里的TutorialsPoint上学习该语言。我目前很难理解这两段代码之间的差异。
interface Person{
age: number;
}
interface Musician extends Person{
instrument: string;
}
var drummer:Musician = {
instrument: "drum",
age: 28
}
Run Code Online (Sandbox Code Playgroud)
和
interface Person{
age: number;
}
interface Musician extends Person{
instrument: string;
}
var drummer = <Musician>{}
drummer.instrument = "drum"
drummer.age = 28
Run Code Online (Sandbox Code Playgroud)
两者有什么区别?是否有特定情况最好使用第一个/第二个实现?
谢谢。
我试图了解 ashared_ptr p在构造未命名时的用法shared_ptr以及它对 的影响p。我正在研究自己的示例并编写了以下代码:
shared_ptr<int> p(new int(42));
cout << p.use_count() << '\n';
{
cout << p.use_count() << '\n';
shared_ptr<int>(p);
cout << p.use_count() << '\n';
}
cout << p.use_count() << '\n';
Output:
1
1
0
1
Run Code Online (Sandbox Code Playgroud)
p第 5 行用于创建临时变量是否正确。shared_ptr(即未命名的shared_ptr)?use_count增加。在我们退出第 7 行的块之前,temp.object 是否已被销毁。p块内的使用计数变为零,为什么退出块后它又变成1了?shared_ptr q如果我在第 5 行使用命名,即:
shared_ptr<int>q(p);
Run Code Online (Sandbox Code Playgroud)
一切都会按预期工作,在第 5 行之后的块内,使用计数将为 2,在我们退出该块后,它将再次为 1。
C++ 有一个特性,类内定义的友元函数只能通过 ADL(参数相关查找)找到:
struct Foo {
friend void fn(Foo) { } // fn can only be called by ADL, it won't be found by other lookup methods
};
Run Code Online (Sandbox Code Playgroud)
对于非友元函数是否可以实现相同的效果?我问这个问题是因为有时,我希望拥有“仅由 ADL 找到”的功能,但我实际上并不需要朋友访问类内部。
(还有一个有点固执己见的问题:如果这是不可能的,那么原因是什么?这个“仅由 ADL 发现”规则是故意设计到语言中的吗?)
c++ declaration friend-function name-lookup unqualified-name
有时用函数表示值更容易,而 lambda 很适合这样做。但是有没有办法从 lambda 声明中返回值呢?
例如:
int i = []{return 2;};
Run Code Online (Sandbox Code Playgroud)
产生错误。如何使 lambda 声明返回2而不添加任何新代码行?
为什么要用 * 来声明指针?
它删除了间接寻址,但当您声明像 之类的指针时,不会删除任何间接寻址int *a = &b,难道它不应该删除 的间接寻址吗&b?
typedef VLA 是否需要计算大小表达式?
int f(void);
int main(void)
{
typedef int (T) [ f() ]; // is f required to be evaluated ?
T x;
return sizeof x;
}
Run Code Online (Sandbox Code Playgroud)
指向 VLA 的typedef指针是否需要计算大小表达式?
int f(void);
int main(void)
{
typedef int (*T) [ f() ]; // is f required to be evaluated ?
T x;
return sizeof x;
}
Run Code Online (Sandbox Code Playgroud)
UPD。在 isvisible 的定义中f,那么对它的调用可能会被优化掉:
int f(void)
{
return 4;
}
int main(void)
{
typedef int (*T) [ f() ];
return sizeof(T); …Run Code Online (Sandbox Code Playgroud) 我正在编写一个打字稿函数,它接受数字数组(即type: number[])并计算其平均值。此外,我想考虑输入数组何时可能包含某些null值。为此,我添加了一个参数,当设置为 时true,告诉函数null在计算平均值之前删除 s。
但我无法找出执行此操作的正确方法,因为我无法覆盖函数内的输入。
这是我的代码calcMean()
function calcMean(arr: number[], nullRemove: boolean = true): number {
if (nullRemove) { // if TRUE, which is the default, then throw out nulls and re-assign to `arr`
const arr: number[] = arr.filter((elem) => elem !== null);
}
// then simply calculate the mean of `arr`
return arr.reduce((acc, v, i, a) => acc + v / a.length, 0); // https://stackoverflow.com/a/62372003/6105259
}
Run Code Online (Sandbox Code Playgroud)
然后我得到一个错误:
在声明之前使用块范围变量“arr”。ts(2448)
我也尝试使用 …
三个主要编译器(gcc、clang、MSVC)愉快地编译了以下示例(godbolt):
struct Test
{
// Member type `Test::Test` is supposedly introduced at this point
// as an alias to `struct Test`, per
// https://timsong-cpp.github.io/cppwp/n4659/class#2
// Shouldn't the following declaration be rejected since it redefines
// the name `Test` in the same scope?
// https://timsong-cpp.github.io/cppwp/n4659/basic.lookup#3
int Test;
// Doing the same explicitly would be a compilation error, as expected:
// using Probe = int;
// int Probe;
};
int main()
{
Test x;
x.Test = 4;
}
Run Code Online (Sandbox Code Playgroud)
上面的例子格式正确吗?以及为什么。 …
即这应该没问题:
Object obj("param");
Run Code Online (Sandbox Code Playgroud)
但这不应该:
Object("param");
Run Code Online (Sandbox Code Playgroud)
有没有办法防止第二种情况发生?
看一下这段代码,它想要释放 C 中链表内所有分配的节点:
ptr = list;
while (ptr != NULL)
{
node *next = ptr->next;
free(ptr);
ptr = next;
}
Run Code Online (Sandbox Code Playgroud)
原因是,在释放当前节点之前,我们需要一个临时变量 next 来存储指向下一个节点的指针。
你看到它node *next = ptr->next;在循环内部了吗?这样每次循环运行时,都会创建一个新的临时节点来存储指向下一个节点的指针。如果我们在循环外部创建变量节点并在循环内部重新分配它,是不是更好?
像这样:
ptr = list;
node *next = NULL;
while (ptr != NULL)
{
next = ptr->next;
free(ptr);
ptr = next;
}
Run Code Online (Sandbox Code Playgroud)
这样效率会更高吗?
declaration ×10
c++ ×6
c ×3
function ×2
syntax ×2
typescript ×2
angular ×1
c++11 ×1
c++17 ×1
class ×1
constructor ×1
expression ×1
free ×1
lambda ×1
linked-list ×1
malloc ×1
name-lookup ×1
object ×1
pointers ×1
scope ×1
shared-ptr ×1
typedef ×1