我试图将类中的成员函数传递给一个带有成员函数类指针的函数.我遇到的问题是我不确定如何使用this指针在类中正确执行此操作.有没有人有建议?
这是传递成员函数的类的副本:
class testMenu : public MenuScreen{
public:
bool draw;
MenuButton<testMenu> x;
testMenu():MenuScreen("testMenu"){
x.SetButton(100,100,TEXT("buttonNormal.png"),TEXT("buttonHover.png"),TEXT("buttonPressed.png"),100,40,&this->test2);
draw = false;
}
void test2(){
draw = true;
}
};
Run Code Online (Sandbox Code Playgroud)
函数x.SetButton(...)包含在另一个类中,其中"object"是模板.
void SetButton(int xPos, int yPos, LPCWSTR normalFilePath, LPCWSTR hoverFilePath, LPCWSTR pressedFilePath, int Width, int Height, void (object::*ButtonFunc)()) {
BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);
this->ButtonFunc = &ButtonFunc;
}
Run Code Online (Sandbox Code Playgroud)
如果有人对如何正确发送此功能有任何建议,以便我以后可以使用它.
我习惯于做Java编程,在编程时你永远不必考虑指针.但是,目前我正在用C++编写程序.在创建具有其他类成员的类时,何时应该使用指针,何时不应该使用指针?例如,我什么时候想要这样做:
class Foo {
Bar b;
}
Run Code Online (Sandbox Code Playgroud)
与此相反:
class Foo {
Bar* b;
}
Run Code Online (Sandbox Code Playgroud) 我正在寻求通过编写示例软件渲染器来提高我的C++技能.它采用由3d空间中的点组成的对象,并将它们映射到2d视口,并为视图中的每个点绘制不同大小的圆.哪个更好:
class World{
vector<ObjectBaseClass> object_list;
public:
void generate(){
object_list.clear();
object_list.push_back(DerivedClass1());
object_list.push_back(DerivedClass2());
Run Code Online (Sandbox Code Playgroud)
要么...
class World{
vector<ObjectBaseClass*> object_list;
public:
void generate(){
object_list.clear();
object_list.push_back(new DerivedClass1());
object_list.push_back(new DerivedClass2());
Run Code Online (Sandbox Code Playgroud)
?? 在第二个例子中使用指针来创建新对象会破坏使用向量的点,因为向量会在第一个示例中自动调用DerivedClass析构函数而不是在第二个示例中调用DerivedClass析构函数吗?在使用向量时是否需要指向新对象的指针,因为只要您使用其访问方法,它们本身就会处理内存管理?现在让我们说我在世界上有另一种方法:
void drawfrom(Viewport& view){
for (unsigned int i=0;i<object_list.size();++i){
object_list.at(i).draw(view);
}
}
Run Code Online (Sandbox Code Playgroud)
调用时,这将为世界列表中的每个对象运行draw方法.假设我希望派生类能够拥有自己的draw()版本.为了使用方法选择器( - >),列表是否需要指针?
例如,
class Person{
string name;
public:
T& operator*(){
return name;
}
bool operator==(const Person &rhs){
return this->name == rhs.name;
}
bool operator!=(const Person &rhs){
return !(*this == rhs); // Will *this be the string name or the Person?
}
}
Run Code Online (Sandbox Code Playgroud)
如果*this最终提领this的string,而不是一个Person,是有维持的使用一种解决方法*作为类外引用操作?
如果我*不放弃而不放弃使用,那将是一个很大的障碍*this.
我搜索并搜索了一个答案,但找不到任何我真正"得到"的东西.
我对c ++非常陌生,无法理解使用双指针,三指针等等.他们有什么意义?
任何人都可以启发我
我正在开发一个项目,我需要我的CUDA设备在包含指针的结构上进行计算.
typedef struct StructA {
int* arr;
} StructA;
Run Code Online (Sandbox Code Playgroud)
当我为结构分配内存然后将其复制到设备时,它只会复制结构而不是指针的内容.现在我通过首先分配指针来解决这个问题,然后将主机结构设置为使用新指针(位于GPU上).以下代码示例使用上面的结构描述了此方法:
#define N 10
int main() {
int h_arr[N] = {1,2,3,4,5,6,7,8,9,10};
StructA *h_a = (StructA*)malloc(sizeof(StructA));
StructA *d_a;
int *d_arr;
// 1. Allocate device struct.
cudaMalloc((void**) &d_a, sizeof(StructA));
// 2. Allocate device pointer.
cudaMalloc((void**) &(d_arr), sizeof(int)*N);
// 3. Copy pointer content from host to device.
cudaMemcpy(d_arr, h_arr, sizeof(int)*N, cudaMemcpyHostToDevice);
// 4. Point to device pointer in host struct.
h_a->arr = d_arr;
// 5. Copy struct from host to device.
cudaMemcpy(d_a, h_a, sizeof(StructA), …Run Code Online (Sandbox Code Playgroud) 这个网站上的第一个计时器,所以这里..
我是C++的新手,我目前正在阅读"DS Malik的数据结构使用C++第二版"一书.
在书中,Malik提供了两种创建动态二维数组的方法.在第一种方法中,将变量声明为指针数组,其中每个指针都是整数类型.恩.
int *board[4];
Run Code Online (Sandbox Code Playgroud)
..然后使用for循环创建'列',同时使用指针数组作为'行'.
第二种方法,您使用指针指针.
int **board;
board = new int* [10];
Run Code Online (Sandbox Code Playgroud)
等等
我的问题是:哪种方法更好?**方法对我来说更容易可视化,但第一种方法可以大致相同的方式使用.两种方式都可用于制作动态二维数组.
编辑:上面的帖子不够清楚.这是我尝试过的一些代码:
int row, col;
cout << "Enter row size:";
cin >> row;
cout << "\ncol:";
cin >> col;
int *p_board[row];
for (int i=0; i < row; i++)
p_board[i] = new int[col];
for (int i=0; i < row; i++)
{
for (int j=0; j < col; j++)
{
p_board[i][j] = j;
cout << p_board[i][j] << " ";
}
cout << endl;
}
cout << …Run Code Online (Sandbox Code Playgroud) 来自Rust指南:
要取消引用(获取引用的值而不是引用本身)
y,我们使用星号(*)
所以我做到了:
fn main() {
let x = 1;
let ptr_y = &x;
println!("x: {}, ptr_y: {}", x, *ptr_y);
}
Run Code Online (Sandbox Code Playgroud)
即使没有明确的解引用,这也给了我相同的结果(x = 1; y = 1):
fn main() {
let x = 1;
let ptr_y = &x;
println!("x: {}, ptr_y: {}", x, ptr_y);
}
Run Code Online (Sandbox Code Playgroud)
为什么?不应该ptr_y打印内存地址并*ptr_y打印1?是否有某种自动解除引用或者我错过了什么?
今天我读了一个让我感到困惑的C片段:
#include <stdio.h>
int
main(void)
{
int a[] = {0, 1, 2, 3};
printf("%d\n", *(*(&a + 1) - 1));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我看来,&a + 1毫无意义,但它运行没有错误.
有人可以解释一下这意味着什么,谢谢.K&R C圣经是否涵盖了这一点?
UPDATE0:读完答案后,我意识到这两个表达式主要让我困惑:
&a + 1,在SO中被问到:关于c中的表达"&anArray"
*(&a + 1) -1,这与阵列衰减有关.
让我们考虑一个对象foo(可能是一个int,一个double,一个习惯struct,一个class,等等).我的理解是,通过foo引用函数(或只是传递指针foo)来传递更高的性能,因为我们避免制作本地副本(如果foo很大则可能很昂贵).
但是,从这里的答案来看,似乎64位系统上的指针实际上可以预期大小为8字节,无论指向什么.在我的系统上,a float是4个字节.这是否意味着如果foo是类型float,那么仅通过值传递而不是给它指向它是更有效的foo(假设没有其他约束可以使得使用一个比函数内的另一个更有效)?
c++ pointers pass-by-reference pass-by-value pass-by-pointer