当我在 if 语句内声明任何内容时,它不会从其中传播出来,此外,如果我在外部有一个变量,并且我在 if 语句内重新声明它,一旦代码结束该语句,它就会丢失它,我如何才能设法全球化if 语句的范围。
抱歉,如果这是一个愚蠢的问题,我是 JavaScript 和 React 新手。需要使用 UseCallback 来包装回调函数以避免重新创建函数,因为它是在功能组件中定义的,每次状态更改时都会重新运行。为什么我们不直接在功能组件之外定义回调来避免这个问题呢?
// define handleClick here instead?
// const handleClick ...
function MyComponent() {
// handleClick is re-created on each render
const handleClick = () => { console.log('Clicked!'); };
}
Run Code Online (Sandbox Code Playgroud) 所以我很好奇在 C++ 中是否可以通过引用将变量或对象从一个文件传递到另一个文件,如下面的示例所示。
字符串示例:
int main() {
//
std::cout << GetName() << std::endl;
}
std::string name = "Paul";
std::string& GetName() {
return name;
}
Run Code Online (Sandbox Code Playgroud)
对象示例:
int main() {
//
std::cout << GetEmployee().GetAge() << std::endl;
}
Employee employee;
Employee& GetEmployee() {
return employee;
}
Run Code Online (Sandbox Code Playgroud) 我对 Qt Creator 非常陌生,并且有一个关于按钮文本更新没有在我期望的时间发生的原因的问题。下面是显示按钮事件的代码片段。该按钮启动另一个外部进程 (AVRDUDE),该进程依次读取通过 USB 连接的 Arduino 板的 EEPROM 的内容。然后数据被处理并显示在 UI 中。由于读取 EEPROM 的过程需要几秒钟才能执行,因此我想将调用此例程的 UI 按钮从“READ FROM EEPROM”更改为“READING !!” 在执行该例程期间。例程完成后,将按钮标签返回到“READ FROM EEPROM”。这是代码
void MainWindow::on_READ_FROM_EEPROM_clicked() // Read EEPROM into file "fromEEPROM.bin", then output values to UI
{
ui->READ_FROM_EEPROM->setText("READING !!");
call_AVRDUDE_read();
int mBufferLength = 1024; // AVR 1K EEPROM space in 328p
char mBuffer[mBufferLength];
float fB[mBufferLength/4];
QString filename = "fromEEPROM.bin";
QFile mFile(filename);
ui->fileCurrentlyDisplayed->setText(filename); // make this filename visable in UI
if (mFile.exists())
{
if (mFile.open(QFile::ReadOnly))
while (!mFile.atEnd())
mFile.read(mBuffer,sizeof(mBuffer));
mFile.close();
memcpy(&fB, &mBuffer, …Run Code Online (Sandbox Code Playgroud) 在 C 函数原型范围内声明形式参数有什么意义?
\n从我自己的测试来看,原型范围中形式参数的声明点似乎遵循从左到右的顺序(参数名称按从左到右的顺序在原型范围内可见),但我不是当然。您能解释一下这一点或指出我正确的地方吗?
\n案例1:gcc对c99标准没有投诉
\nint sum(int n, int a[n]); \nRun Code Online (Sandbox Code Playgroud)\n情况 2:来自带有 c99 标准的 gcc 的“错误:\xe2\x80\x98n\xe2\x80\x99 此处未声明(不在函数中)”
\nint sum(int a[n], int n);\nRun Code Online (Sandbox Code Playgroud)\n上述两种情况都放置在文件作用域下,文件作用域中没有名为 n 的变量。
\n例如,在 Python 中我们有一个非局部特征:
nonlocal 关键字用于处理嵌套函数内的变量,其中变量不应属于内部函数。使用关键字nonlocal 来声明该变量不是局部的。
nonlocal 语句声明,每当我们更改名称 var 的绑定时,绑定都会在已绑定 var 的第一帧中更改。回想一下,如果没有非本地语句,赋值语句将始终在当前环境的第一帧中绑定名称。nonlocal 语句指示名称出现在环境中除第一个(局部)帧或最后一个(全局)帧之外的某个位置。
Dart中有类似的东西吗?
以下是来自 Python 的代码示例:
def make_withdraw(balance):
"""Return a withdraw function that draws down balance with each call."""
def withdraw(amount):
nonlocal balance # Declare the name "balance" nonlocal
if amount > balance:
return 'Insufficient funds'
balance = balance - amount # Re-bind the existing balance name
return balance
return withdraw
Run Code Online (Sandbox Code Playgroud)
我无法使用的 Dart 伪翻译nonlocal:
makeWithdraw(balance) {
//Return a withdraw function that draws down balance with each call. …Run Code Online (Sandbox Code Playgroud) 免责声明:我是 Qt 的新手。
假设我们有一个从函数返回的字节数组innerFunc,稍后在另一个函数中使用outerFunc。
QByteArray innerFunc(){
QProcess ls;
ls.start("ls", QStringList() << "-1");
return ls.readAll();
}
void outerFunc(){
QByteArray gotcha = innerFunc();
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
在 vanilla c++ 中,我希望readAll函数返回一个稍后需要删除的指针。在 Qt 中,这个函数返回该类的一个实例QByteArray,所以我想它不应该在innerFunc的范围之外访问。
如果是这样,我应该如何正确地将数据传输到外部函数?应该复制QByteArray *tmp = new QByteArray还是没有必要?
所以我最近刚接触 Python,但我似乎能够编写一些东西并让它工作。然而,我一直在尝试扩展我对语言中事物如何工作的知识,而将这个简单的文件放在一起让我感到困惑。
class TestA:
def __init__(self):
self.varNum = 3
def printNum(self):
print(self.varNum)
class TestB:
varNum = 0
def __init__(self):
varNum = 3
def printNum(self):
global varNum
print(varNum)
a = TestA()
a.printNum()
b = TestB()
b.printNum()
Run Code Online (Sandbox Code Playgroud)
TestA将3 正确打印到屏幕的代码。然而,代码TestB却给了我NameError这样的说明:'varNum' is not defined。无论我是否有global varNum这条线,我都会收到该错误。
我想令我困惑的是我将该__init__函数视为类构造函数。当我使用 Java 或 C# 等语言进行编程时,我在构造函数之外声明了全局变量,以便它们的作用域是整个类。这不是 Python 中的东西吗?我编写的代码只是标记self.了所有内容,因为我只是想快速地将一些东西放在一起,但我现在正在尝试更多地了解该语言。self.Python 中创建类作用域变量的唯一方法是什么?或者还有其他方法吗?
谢谢你的时间 :)
我对 C# 很陌生,并且对变量范围感到困惑。这是我编写的代码块,但是当我尝试在 try 块之外访问 a 和 b 的值时,它给了我编译时错误
class TestConditionalStatements
{
static void Main(string[] args)
{
int a, b;
try
{
a = 10;
b = 20;
}
catch (Exception e)
{
Console.Write(e.Message);
}
//This line gives compile time error
ConditionalStatements c = new ConditionalStatements(a, b);
string result;
c.checkValidity(c, out result);
Console.WriteLine(result);
}
}
Run Code Online (Sandbox Code Playgroud) class PowTwo:
"""Class to implement an iterator
of powers of two"""
def __init__(self, max=0):
self.max = max
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n <= self.max:
result = 2 ** self.n
self.n += 1
return result
else:
raise StopIteration
a = PowTwo(3)
b = iter(a)
print(next(a))
Run Code Online (Sandbox Code Playgroud)
没有这个片段b = iter(a),输出是
Traceback (most recent call last):
File "/Users/Mark/test2.py", line 20, in <module>
print(next(a))
File "/Users/Mark/test2.py", line 13, in __next__
if self.n <= self.max:
AttributeError: 'PowTwo' object …Run Code Online (Sandbox Code Playgroud)