我想在类函数中传递类变量 a 作为默认参数。我可以通过使用 #define 来做到这一点,但我想通过使用类成员来做到这一点。有没有办法做到这一点?
#include <iostream>
class Test
{
private:
int a=5;
public:
Test(){}
void function(int value=a){ //i want to use class variable a here.
std::cout<<value<<std::endl;
}
~Test(){}
};
int main()
{
Test var1;
var1.function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序显示错误:无效使用非静态数据成员
我希望这个程序返回一个数组,然后是一个来自main的变量来从函数中获取这个数组,并且可以在一部分代码中使用,该代码必须根据初始变量中的这些值来验证字符串.但是变量没有得到返回的数组.我看过一些帖子说你不能在C中返回数组,但我也看过其他帖子,这是可能的.
但是我想知道如何捕获这个返回值.
int getUsers()
{
char userVect[5][25];
char user[24];
int i = 0, j = 0, k;
FILE *usernames;
usernames = fopen("usernames.cfg", "r");
if (usernames == NULL){
perror("usernames - err");
return(-1);
}
while(fgets(user, sizeof(user), usernames) !=NULL){
strcpy(userVect[j], user);
j++;
}
fclose(usernames);
for(k=0; k<j; k++)
printf("Usernames are: %s\n", userVect[k]);
return userVect;
}
int main()
{
printf("\nWelcome to Sys v1.24. To start off, please insert your command. \n");
char *users;
users = getUsers();
printf("%c\n", users);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我实现了一个应该是线程安全的共享库.保护我std::lock_guard<std::mutex>在C++ 11标准中使用的关键区域.
错字是我省略了对象本身:
std::lock_guard<std::mutex>(getMutexObj());
Run Code Online (Sandbox Code Playgroud)
代替
std::lock_guard<std::mutex> lock_obj(getMutexObj());
Run Code Online (Sandbox Code Playgroud)
并在每个地方复制/粘贴它...不用说,当多线程应用程序开始无法预测崩溃时,我花了一段时间才导致它.
只是为了涵盖所有要点,声明getMutexObj()和互斥体本身如下:
...
mutable std::mutex m_mutex;
...
std::mutex& getMutexObj() const
{
return m_mutex;
}
Run Code Online (Sandbox Code Playgroud)
g++ 5.2.0使用以下警告标志编译所有代码:
WARNINGS := -pedantic \
-Wall \
-Wextra \
-Werror \
-Wconversion \
-Woverloaded-virtual \
-Wcast-qual \
-Wctor-dtor-privacy \
-Wdisabled-optimization \
-Wuninitialized \
-Wformat=2 \
-Winit-self \
-Wlogical-op \
-Wmissing-declarations \
-Wmissing-include-dirs \
-Wold-style-cast \
-Wredundant-decls \
-Wshadow \
-Wsign-conversion \
-Wsign-promo \
-Wstrict-null-sentinel \
-Wstrict-overflow=5 \
-Wswitch-default \
-Wundef \
-Wunused \ …Run Code Online (Sandbox Code Playgroud) 假设以下代码
struct a {
unsigned cntr;
};
void boo(struct a *v) {
v->cntr++;
while(v->cntr > 1);
}
Run Code Online (Sandbox Code Playgroud)
我想知道由于C11标准中的以下语句,是否允许编译器省略while内部循环:boo()
控制表达式不是常量表达式的迭代语句,156)不执行输入/输出操作,不访问易失性对象,并且在其主体、控制表达式中不执行同步或原子操作,或者(在 for 的情况下)语句)其表达式-3,可以假定由实现终止。157)
157)这是为了允许编译器进行转换,例如即使无法证明终止也可以删除空循环。
在控制表达式中,是否可以v->cntr将 视为同步,因为v可能是指向可以在外部修改(例如由另一个线程)的全局结构的指针?
附加问题。如果未定义为,是否允许编译器v->cntr在每次迭代时不重新读取?vvolatile
我试图用 unsigned long long 数据类型在 C 中声明 64 位变量,但它不起作用并且行为类似于 uint32 位变量。访问/检查 32 以上位的解决方案是什么?
#include <stdio.h>
#include <stdint.h>
int main() {
// Write C code here
printf("Hello world");
unsigned long long temp = 0x1234567890123456ULL;
unsigned long long temp2 = temp >> 1;
uint64_t var = 0x1234567890123456;
printf("\n%x", (long long) temp);
printf("\n%x", (long long) temp2);
printf("\n%x", 1ULL << 31ULL);
printf("\n%x", 1ULL << 32ULL);
printf("\n%x", var);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
你好,世界
90123456
48091a2b
80000000
0
90123456