我的代码在c和c++中的行为是不同的。
void *(*funcPtr)() = dlsym(some symbol..) ; // (1) works in c but not c++
int (*funcPtr)();
*(void**)(&funcPtr) = dlsym(some symbol..) ; // (2) works in c++
我不明白为什么第二次铸造工作在c++,而第一次铸造不工作在c++。在c++中,(1)显示的错误消息从void*到void*()的转换是无效的。
我有一段代码可以在C中工作,但不能在C++中工作,有没有办法让它在C和C++上都能工作呢?
void foo(void* b)
{
int *c = b;
printf("%d\n",*c);
}
int main ()
{
int a = 1000;
foo(&a);
return 0;
}
输出:
C++:
1 In function 'void foo(void*)':
2 Line 3: error: invalid conversion from 'void*' to 'int*'
3
第1行导致error: conversion from ‘C<void()>’ to non-scalar type ‘C<void (*)()>’ requested。我知道我可以将它写成第2行,但是如何使用make_class()并将它赋值给一个变量呢?
#include <iostream>
using namespace std;
template<class T> class C {
T f;
public:
C(T ff) : f(ff) {}
};
template<class Ft> C<
在C中有一件事对我来说总是很奇怪,当在函数参数中使用函数指针时,为什么发送函数名与发送函数地址是一样的?
void bar(void (*functionPtr)())
{
doSomething
}
void foo(void)
{
doSomething
}
int main()
{
bar(&foo);
bar(foo); // why is this the same? In C logic it's not supposed to work
return (0);
}
以下情况:
class Main{
void MainMethod(){
C * c;
B * b = new B();
b->fillC(c);
}
};
class B{
void fillC(C* c){
c = new C();
}
};
class C{
};
在我的软件中,我遇到了这种情况。在程序的末尾,类Main的c仍然是空的。为什么会这样呢?
我正在阅读一些开源C代码,并多次遇到A a = (A) b;类型转换.例如,
static void hexdump(const void* pv, int len)
{
const unsigned char* p = (const unsigned char*) pv;
// some other code
}
A a = (A) b;代码主要发生在b是指针时,void *指针最常见。我有C++背景。我认为在C++中,赋值操作符会自动处理=的类型转换?因为它已经知道a是A类型的。
在C中是否需要显式类型转换?
因此,我了解了如何将地址传递给C函数以避免传入值。但我不理解的是在C++中通过引用传入。我的理解不足来自于C,在C中,如果你通过引用传递变量,你只会得到一个基于地址的无意义的int值。例如,如果你这样做了:
int x = 5;
int* p = x;
int y = p;
y将是一个无意义的int值。因此,我无法理解基于上述逻辑的C++通过引用传递。有没有一些编译器标识你正在尝试做的事情,以便它以一种使其与C代码等效的方式进行编译?有关按指针和按引用传递C和C++的信息,请参见下面的内容:
// C pass by pointer value
void main() {
int a
我使用了一些内置的函数fun(void **args),并通过参数fun(args);调用这个函数,那么void **args是什么意思,以及如何检查args在C中的值是什么。
void GetValues(void ** args, nsString sToken, int iTokenNo)
{
printf("Token is : %s and Token no is %d ",sToken,iTokenNo);
NSLog("Token Name %s and Token no is %d",sToken,iTokenNo);
}
在
class A {
public:
void show() {
cout << "Class A Show"<<'\n';
}
};
class C : public A {
public:
void show() {
cout << "Class C Show" << '\n';
}
};
int main() //first int main()
{
C* a = new C; //line 1