我正在使用一个名为Pointer的类,我猜这是一个真实指针的包装器.我认为这个类中的这行代码使我能够得到真正的指针:
operator const T* () const;
Run Code Online (Sandbox Code Playgroud)
这究竟是什么意思?我怎么称呼这个?
假设myPointer是一个Pointer<int16_t>对象.我应该能够int_16*通过使用上面的运算符重载来获取包装此指针的对象,但我不知道如何.
编辑
根据答案,我现在知道我可以这样做:
const int16_t* myRealPointer = myPointer;
Run Code Online (Sandbox Code Playgroud)
现在假设我需要调用一个需要int16_t*参数的函数(所以没有const).如何将此myRealPointer对象传递给该函数?
我正在尝试检查指针是否指向某个字符.
像这样:
#include<stdio.h>
#include<string.h>
#define a 3
int func(char *);
int main()
{
char *s="a";
int type;
type=func(s);
printf("%d",type);
return 0;
}
int func(char *s)
{
int type;
if(*s=="a")
{
type=1;
}
return type;
}
Run Code Online (Sandbox Code Playgroud)
但我经常收到警告:警告:指针与整数之间的比较if(*s =="a")
是否可以比较指针和整数?
还有另一种方法可以解决这个问题吗?
我可以找到哪个字母指向*s而不打印它?
int main(int argc, char * argv[]) {
int a = 10;
int * sp = &a;
int * * dp1 = &sp;
int * * dp2 = &&a; // NG
int * * dp3 = &(&a); // NG
int * * dp4 = &((int *) &a); // NG
}
Run Code Online (Sandbox Code Playgroud)
$ cc test.c
test.c: In function ‘main’:
test.c:6:17: error: lvalue required as unary ‘&’ operand
int * * dp3 = &(&a); // NG
^
test.c:7:17: error: lvalue required as unary ‘&’ …Run Code Online (Sandbox Code Playgroud) 我按照V. Romeo的实体管理教程(在GitHub和Youtube上).
然后我尝试重写类CEntity,CComponent和测试CPosition(主要来自Romeo视频/代码的内存).我遇到的问题是,在我的主要我在堆栈上创建一个CEntity并添加一个组件.当我通过i添加组件时,addComponent()获取对新创建的组件的引用,返回addComponent().
当我现在想要通过返回的引用修改组件时,我所做的更改不会反映回实体(组件).看起来像是对我的悬空参考,但我无法找到我犯的错误.
谁能指出我在这里做错了什么?
掌握我的CEntity课程:
#include <array>
#include <bitset>
#include <memory>
#include <cassert>
#include <stdexcept>
namespace inc
{
using ComponentID = unsigned int;
ComponentID getNewID()
{
static ComponentID id = 0;
return id++;
}
template <typename T>
ComponentID getComponentID()
{
static ComponentID component_id = getNewID();
return component_id;
}
// Forward declarations used by CEntity:
struct CComponent;
class CEntity
{
public: …Run Code Online (Sandbox Code Playgroud) 是否有充分的理由将函数的指针传递给C中的另一个函数.我没有看到函数指针的一般用例.
如果有人可以给出一些用例,其中函数的指针是实现某些特定用例的唯一方法,这将是很好的.
我正在编写一个包含与以下类似结构的小型c ++程序:
class A {
B * someObjects;
};
typedef A* APointer;
struct B{
APointer a;
int n;
}
Run Code Online (Sandbox Code Playgroud)
尝试编译这会给出"标识符未定义"错误,因为结构B在类A中是未知的.否则在类A之前声明结构B应该仍然给出类似的错误,因为那时B不知道APointer,或者APointer不知道A有没有可能让A级和B级成为好朋友?提前致谢!
我制作了以下链表结构和printList函数.两者都正常运行:
struct Node{
int data;
Node *np;
};
void printList(Node *x){
cout << x->data << " ";
if (x->np != NULL){
printList(x->np);
}
return;
}
Run Code Online (Sandbox Code Playgroud)
然后我决定编写一个递归函数来复制链表.一个实现,返回指针值,工作...而另一个,返回一个地址 - 不起作用...我不能为我的生活弄清楚为什么会这样:
这有效:
Node * copyList(Node *x){
Node * y = new Node;
y->data = x->data;
if (x->np != NULL){
y->np = copyList(x->np);
}else{
y->np = NULL;
}
return y;
}
Run Code Online (Sandbox Code Playgroud)
这不起作用:
Node * copyList(Node *x){
Node y = {x->data,NULL};
if (x->np != NULL){
y.np = copyList(x->np);
}
return &y;
}
Run Code Online (Sandbox Code Playgroud)
我有点困惑为什么.我会假设一个指针本质上是指一个内存地址,返回&y就好了......
在编译和执行以下程序时,我在编译期间收到警告并在执行期间发生seg错误.
警告
program.c: In function main:
program.c:17: warning: passing argument 2 of convertString from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
第17行是打电话给 convertString(input, &output);
附加调试器可以看到在下面的行发生了段错误
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400521 in convertString (input=0x7fffffffd100 "abcdefg", output=0x7fffffffd0f0) at program.c:9
9 **output = *input;
(gdb) s
#include<stdio.h>
#include<string.h>
void convertString(char *input, char **output)
{
if(strlen(input)== 0)
{
return;
}
**output = *input;
(*output)++;
convertString(input+1,output);
}
int main()
{
char input[] = "abcdefg";
char output[sizeof(input)];
convertString(input, &output);
printf("%s", output);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请帮助我在哪里做错了.
我正在编写一些用于从双向链表中删除节点的代码,我遇到了以下语法.虽然我能够理解语法正在做什么但是不能得到top get下面的代码如何可能
struct dll{
int data;
struct dll *next;
struct dll *prev;
};
Run Code Online (Sandbox Code Playgroud)
并且在某些功能中有这种语法
else{
temp2=temp->prev;
temp2->next=temp->next;
temp->next->prev=temp2;
free(temp);
}
Run Code Online (Sandbox Code Playgroud)
我知道temp->next它使temp指针指向的是什么next node,从语法中我也可以理解,temp->next->prev这会使指针指向prev pointer of the next node.
但问题是如何评估这种语法?
pointers ×10
c ×6
c++ ×4
char ×2
struct ×2
class ×1
dereference ×1
linked-list ×1
operators ×1
reference ×1
typedef ×1
unique-ptr ×1
wrapper ×1