我想要一些代码来编译我的TextBox中的代码(例如).我的意思是我想在运行程序后编译代码.我怎样才能做到这一点?
请参阅下面的简单示例.当一个函数返回一个enum
被分配给一个不同的变量enum
我甚至没有得到任何警告gcc -Wall -pedantic
.为什么C编译器不能对enum
s 进行类型检查?还是gcc
具体的?我现在无法访问任何其他编译器来试用它.
enum fruit {
APPLE,
ORANGE
};
enum color {
RED,
GREEN
};
static inline enum color get_color() {
return RED;
}
int main() {
enum fruit ftype;
ftype = get_color();
}
Run Code Online (Sandbox Code Playgroud) 这是一个在线C++测试问题,已经完成.
#include<iostream>
using namespace std;
class A
{
};
class B
{
int i;
};
class C
{
void foo();
};
class D
{
virtual void foo();
};
class E
{
int i ;
virtual void foo();
};
class F
{
int i;
void foo();
};
class G
{
void foo();
int i;
void foo1();
};
class H
{
int i ;
virtual void foo();
virtual void foo1();
};
int main()
{
cout <<"sizeof(class A) : " << sizeof(A) << …
Run Code Online (Sandbox Code Playgroud) 我对这种情况感到困惑,谷歌搜索没有给我答案.基本上我有以下不编译的简单代码:
#include <iostream>
class A
{
public:
int a(int c = 0) { return 1; }
static int a() { return 2; }
};
int main()
{
std::cout << A::a() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编制本,GCC 4.2表示调用A::a()
的main()
是与暧昧的两个版本a()
有效候选人.Apple的LLVM编译器3.0编译时没有错误.
为什么gcc对我要调用哪个函数感到困惑?我认为这是明显的,通过资格a()
与A::
我要求的static
功能的版本.当然,如果我删除该static
函数a()
,这段代码仍然无法编译,因为A::a()
它不是用于调用non的有效语法static
a()
.
谢谢你的评论!
我想实现一个不区分大小写的哈希映射.这个问题本身并不新鲜,但我想增加额外的功能,不知道要采取什么样的方向.我希望客户能够做到这样的事情:
boolean preserve_case = true;
Map<String, MyClass> maplet = new CaseInsensitiveHashMap<MyClass>(preserve_case); // If the client enters true at construction, then the put, get, and remove methods should still be case insensitive, but the entry and key sets should preserve the case that the client used when calling put.
maplet.put("FoO", my_class);
MyClass bar = maplet.get("foo"); // Should return a reference to my_class
Set<Entry<String, MyClass>> case_sensitive_set = maplet.entrySet(); // Since the client input true to preserve order, this entry set should be …
Run Code Online (Sandbox Code Playgroud) 我在两个代码中做同样的事情.
在代码1中:我使用了a char *
并使用malloc
in 分配空间main
.
在代码2中:我使用了一个char
数组用于相同的目的.但为什么输出会有所不同?
代码1:
struct node2
{
int data;
char p[10];
}a,b;
main()
{
a.data = 1;
strcpy(a.p,"stack");
b = a;
printf("%d %s\n",b.data,b.p); // output 1 stack
strcpy(b.p,"overflow");
printf("%d %s\n",b.data,b.p); // output 1 overflow
printf("%d %s\n",a.data,a.p); // output 1 stack
}
Run Code Online (Sandbox Code Playgroud)
代码2:
struct node1
{
int data;
char *p;
}a,b;
main()
{
a.data = 1;
a.p = malloc(100);
strcpy(a.p,"stack");
b = a;
printf("%d %s\n",b.data,b.p); //output 1 stack
strcpy(b.p,"overflow");
printf("%d %s\n",b.data,b.p); …
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
#include <unistd.h>
int main(){
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在gcc中编译这个程序并执行它只打印hello-err而不是hello-out.为什么会这样?有人可以解释它背后的原因吗?
在C++中(我使用QT)我可以用两种方式创建QString类的实例:
QString str = "my string";
Run Code Online (Sandbox Code Playgroud)
QString *str = new QString("my string");
Run Code Online (Sandbox Code Playgroud)
我知道这与指针有关.所以我的问题是:
delete str;
.str
使用方法1时如何删除变量?谢谢
我从c ++练习册中复制了这个程序.幕后发生了什么?
预期的产出是:
sum = 30 sum = 70
#include<iostream>
using namespace std;
class M
{
int x;
int y;
public:
void set_xy(int a, int b)
{
x=a;
y=b;
}
friend int sum(M m);
};
int sum (M m);
//so far so good, problem begins from here. what's happening after here?
{
int M ::*px = &M ::x;
int M ::*py = &M ::y;
M *pm =&m;
int s= m.*px+ pm->*py;
return s;
}
int main()
{
M n;
void …
Run Code Online (Sandbox Code Playgroud) 我有一个叫做的线程 mainloop
即
int run_mainloop;
void* mainloop(void* param)
{
// local vars
// initialize local vars
while(run_mainloop)
{
// run mainloop
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
线程从一个叫做的函数中启动client_open
,即
int client_open()
{
run_mainloop = 1;
return pthread_create(&thread, NULL, mainloop, NULL);
}
Run Code Online (Sandbox Code Playgroud)
但是,mainloop
如果初始化局部变量失败,我需要立即通知client_open
提前退出.
pthread_join
是不合适的,因为它会阻止,我不能client_open
阻止.如果要在返回之前等待一小段时间就可以了.
如果不使用会阻塞的pthread_join,我怎么能以一种很好的方式做到这一点.我希望能够获得返回码.