小编Aus*_*oke的帖子

在应用程序中编译C#代码

我想要一些代码来编译我的TextBox中的代码(例如).我的意思是我想在运行程序后编译代码.我怎样才能做到这一点?

c# compilation c#-4.0

25
推荐指数
2
解决办法
2262
查看次数

枚举类型检查C/gcc

请参阅下面的简单示例.当一个函数返回一个enum被分配给一个不同的变量enum我甚至没有得到任何警告gcc -Wall -pedantic.为什么C编译器不能对enums 进行类型检查?还是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 enums gcc

18
推荐指数
2
解决办法
1万
查看次数

在C++中使用int,function,virtual函数的sizeof类?

这是一个在线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)

c++ virtual class object sizeof

16
推荐指数
1
解决办法
2万
查看次数

对重载静态函数的模糊调用

我对这种情况感到困惑,谷歌搜索没有给我答案.基本上我有以下不编译的简单代码:

#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().

谢谢你的评论!

c++

12
推荐指数
1
解决办法
1070
查看次数

适当地支持不区分大小写的映射

我想实现一个不区分大小写的哈希映射.这个问题本身并不新鲜,但我想增加额外的功能,不知道要采取什么样的方向.我希望客户能够做到这样的事情:

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)

java

9
推荐指数
2
解决办法
5725
查看次数

字符数组和指针之间的区别

我在两个代码中做同样的事情.

在代码1中:我使用了a char *并使用mallocin 分配空间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)

c c++ arrays struct pointers

8
推荐指数
1
解决办法
677
查看次数

为什么这个c程序不打印第一个printf语句?

#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 printf

6
推荐指数
1
解决办法
3252
查看次数

哪种是在C++中实例化对象的正确方法?

在C++中(我使用QT)我可以用两种方式创建QString类的实例:

方法1

QString str = "my string";
Run Code Online (Sandbox Code Playgroud)

方法2

QString *str = new QString("my string");
Run Code Online (Sandbox Code Playgroud)

我知道这与指针有关.所以我的问题是:

  1. 两者有什么区别?
  2. 我应该坚持哪种方法?
  3. 什么时候使用方法1是正确的,什么时候使用方法2是正确的?
  4. 在方法2中,我可以通过调用来销毁对象delete str;.str使用方法1时如何删除变量?

谢谢

c++ qt qt4 visual-c++

6
推荐指数
2
解决办法
2825
查看次数

了解指向成员的运营商

我从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)

c++ pointers compiler-errors pointer-to-member dereference

6
推荐指数
1
解决办法
1205
查看次数

如何检测pthread_create提前退出而不会阻塞太长时间?

我有一个叫做的线程 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,我怎么能以一种很好的方式做到这一点.我希望能够获得返回码.

c linux

5
推荐指数
1
解决办法
1511
查看次数