我担心这可能是一个虚拟的问题,但它让我非常难过.
我正在寻找将对象的方法传递给过程的最简单方法,以便过程可以调用对象的方法(例如,在超时之后,或者可能在不同的线程中).基本上我想:
我想我可以使用接口实现相同的效果,但我认为还有另一种方法,因为这个"对象过程"类型声明存在.
以下不起作用,但它可能有助于解释我困惑的地方......?
interface
TCallbackMethod = procedure of object;
TCallbackObject = class
procedure CallbackMethodImpl;
procedure SetupCallback;
end;
implementation
procedure CallbackTheCallback(const callbackMethod: TCallbackMethod);
begin
callbackMethod();
end;
procedure TCallbackObject.CallbackMethodImpl;
begin
// Do whatever.
end;
procedure TCallbackObject.SetupCallback;
begin
// following line doesn't compile - it fails with "E2036 Variable required"
CallbackTheCallback(@self.CallbackMethodImpl);
end;
Run Code Online (Sandbox Code Playgroud)
(一旦问题得到解答,我将删除上述代码,除非它以某种方式帮助解释.)
如果你试图cout指向volatile类型的指针,即使是你通常希望cout打印字符串的volatile指针,你只需要得到'1'(假设指针不是null我认为).我假设输出流operator <<是专门用于volatile指针的模板,但我的问题是,为什么?什么用例激发了这种行为?
示例代码:
#include <iostream>
#include <cstring>
int main()
{
char x[500];
std::strcpy(x, "Hello world");
int y;
int *z = &y;
std::cout << x << std::endl;
std::cout << (char volatile*)x << std::endl;
std::cout << z << std::endl;
std::cout << (int volatile*)z << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hello world
1
0x8046b6c
1
Run Code Online (Sandbox Code Playgroud) 可能重复:
指针的常用用途?
我仍在学习C++的基础知识,但我已经足够了解有用的小程序.
我理解指针的概念,我在教程中看到的例子对我来说很有意义.但是,在实际层面上,作为(前)PHP开发人员,我还不自信在我的程序中实际使用它们.
事实上,到目前为止,我还没有觉得需要使用任何指针.我有我的类和函数,我似乎完全没有使用任何指针(更不用指针指针).我不禁为我的小程序感到有些自豪.
尽管如此,我知道我缺少一个C++最重要的功能,一个双刃:指针和内存管理可能造成破坏,看似随机的崩溃,很难找到错误和安全漏洞......但与此同时,如果使用得当,它们必须允许巧妙而有效的编程.
所以:不要使用指针告诉我我错过了什么.
什么是使用指针必须的好方案?
他们允许你做什么,否则你不能这样做?
它们以哪种方式使您的程序更有效率?
那么指针指针呢?
[编辑:所有各种答案都很有用.SO的一个问题是我们不能"接受"不止一个答案.我经常希望我能.实际上,所有答案的组合有助于更好地理解整体情况.谢谢.]
我从一个简单的cout没有输出,而printf将始终打印数字:
std::cout << variableuint8; // prints nothing
printf("%u", variableuint8); // prints the number
Run Code Online (Sandbox Code Playgroud)
我以前从未遇到过这种行为,虽然我有一个解决方法,但我想了解它.
耶,指针.所以它可能比所述的更多参与.这是上下文 - 我不认为它应该重要,当cout和printf获取信息时,它被解除引用到一个简单的unsigned char.这可能是解决问题所需的更多信息,但我想完全了解它是否相关.
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef struct{
UInt8 len;
// some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;
UInt8 somerandomdata[8];
DeviceNotification * notification;
notification = (DeviceNotification *) somerandomdata;
std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);
Run Code Online (Sandbox Code Playgroud)
随机数据在别处初始化.对于此输出,数组中的第一个字节是0x08:
Len: ()
Len: (8)
Run Code Online (Sandbox Code Playgroud)
我们的C++教授提到使用operator->的结果作为另一个operator->的输入被认为是坏的风格.
所以不要写:
return edge->terminal->outgoing_edges[0];
Run Code Online (Sandbox Code Playgroud)
他宁愿:
Node* terminal = edge->terminal;
return terminal->outgoing_edges[0];
Run Code Online (Sandbox Code Playgroud)
这个答案附带了一个有趣的声明 - "在int*小于a的机器上char*".(让我们排除指向函数的指针)
指向不同类型的指针是否有可能具有不同的大小?为什么这会有用?
它在C++ Primer中有说明
在C++中,指针和数组紧密交织在一起.特别是,正如我们将看到的,当我们使用数组时,编译器通常会将数组转换为指针.
我想使用迭代器来打印数组.下面的程序运行正常但是当我尝试打印时,arr2或者arr3如果我没有弄错,哪种类型int *,我得到一个错误(判断&运算符意味着下面的参考).
error: no matching function for call to ‘begin(int*&)’
int main(int argc, char** argv) {
int arr[] = {0,1,2,3,4,5,6,7,8,9};
auto arr2 = arr;
auto arr3(arr); // I think arr2 and arr3 are of same type
for(auto it = std::begin(arr) ; it != std::end(arr) ; ++it)
std::cout << *it << " ";
std::cout << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
考虑到该语句,如果数组被编译器转换为指针,该程序如何用于打印arr使用的内容,std::begin()并且std::end() 不起作用arr2或者 …
我有一个std::vector<int>指针指向int*向量中的元素.假设指针指向第三个元素:pointer=&vector.at(2).如果我现在对矢量进行混洗,它是否仍然指向相同的元素(第三个),还是它指向新的位置,而这个元素曾经是第三个元素现在已经移动了?
在那之后,我想让问题更加通用:当向量扩展或缩小时,向量中元素的指针和迭代器如何表现?
考虑以下代码:
#include <iostream>
#include <functional>
struct B {
template <class C, class M, class T>
void call1(C (M::*member)(), T *instance) {
std::function<void()> fp = std::bind(member, instance);
fp();
}
template <class C, class M, class T>
void call2(C (M::*member), T *instance) {
std::function<void()> fp = std::bind(member, instance);
fp();
}
void foo() {
call1(&B::func, this); // works
call2(&B::func, this); // works
call1(&B::func2, this); // Error: no matching member function for call to 'call2'
call2(&B::func2, this); // works
}
void func() {
std::cout …Run Code Online (Sandbox Code Playgroud) 处理和尝试恢复C指针的以下哪种方法保证有效?
1)转换为无效指针并返回
int f(int *a) {
void *b = a;
a = b;
return *a;
}
Run Code Online (Sandbox Code Playgroud)
2)转换为适当大小的整数并返回
int f(int *a) {
uintptr_t b = a;
a = (int *)b;
return *a;
}
Run Code Online (Sandbox Code Playgroud)
3)一些简单的整数运算
int f(int *a) {
uintptr_t b = a;
b += 99;
b -= 99;
a = (int *)b;
return *a;
}
Run Code Online (Sandbox Code Playgroud)
4)整数运算非常重要,足以掩盖起源,但仍然会保持价值不变
int f(int *a) {
uintptr_t b = a;
char s[32];
// assume %lu is suitable
sprintf(s, "%lu", b);
b = strtoul(s);
a = (int *)b; …Run Code Online (Sandbox Code Playgroud)