C++类对象指针和访问成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11373139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:05:38  来源:igfitidea点击:

C++ class object pointers and accessing member functions

c++classobjectpointers

提问by rock_win

I'm bit new to C++ and try to work things with Qt and came across this confusing thing:

我对 C++ 有点陌生,并尝试使用 Qt 工作,但遇到了这个令人困惑的事情:

The concepts on various tutorials state something like:

各种教程中的概念陈述如下:

Class *obj;

*obj- will display the value of object stored at the referenced memory
obj- will be address of memory to which it is pointing

*obj- 将显示存储在引用内存中的对象的值
obj- 将是它指向的内存地址

so, I would do something like

所以,我会做类似的事情

*obj=new Class();

but if I want to access a function, I have to do obj->function1();instead of *obj->function1();

但是如果我想访问一个函数,我必须做obj->function1();而不是*obj->function1();

-- not sure why, since with normal objects [ normalObj.function1();] would work, since that's the value directly.

-- 不知道为什么,因为使用普通对象 [ normalObj.function1();] 会起作用,因为那是直接的值。

So, for pointer objects why do we use memory reference to access the function, or is it that in case of normal objects also, its always references

那么,对于指针对象,为什么我们使用内存引用来访问函数,或者在普通对象的情况下,它总是引用

P.S: Can someone guide me to a good tutorial of usage of pointers in C++, so that my queries like these can be directly addressed in it.

PS:有人可以指导我在 C++ 中使用指针的好教程,以便可以直接解决我的此类查询。

回答by Yokatta24

The *symbol is used to define a pointer and to dereference a pointer. For example, if I wanted to create a pointer to an int, I could do:

*符号用于定义指针和取消引用指针。例如,如果我想创建一个指向 int 的指针,我可以这样做:

int *ptr;

int *ptr;

In this example, the *is being used to declare that this is a pointer to an int. Now, when you are not declaring a pointer and you use the *symbol with an already declared pointer, then you are dereferencing it. As you probably know, a pointer is simply an address. When you dereference a pointer, you are obtaining the value that is being pointed to by that address. For example:

在这个例子中,*被用来声明这是一个指向 int指针。现在,当您没有声明一个指针并且您将*符号与一个已经声明的指针一起使用时,那么您就是在取消引用它。您可能知道,指针只是一个地址。当您取消引用一个指针时,您将获得该地址所指向的值。例如:

int pointToMe = 5;
int *ptr = &pointToMe;
std::cout << *ptr;

This will print out 5. Also, if you are assigning a pointer to a new address and it's not in the declaration, you do notuse the *symbol. So:

这将打印出 5。此外,如果您要分配一个指向新地址的指针并且它不在声明中,则不要使用该*符号。所以:

int pointToMe = 5;
int *ptr;
ptr = &pointToMe;

is how you would do it. You can also deference the pointer to assign a new value to the value being pointed to by the address. Such as:

是你会怎么做。您还可以遵循指针以将新值分配给地址指向的值。如:

int pointToMe = 5;
int *ptr = &pointToMe;
std::cout << *ptr; // Prints out 5
*ptr = 27;
std::cout << *ptr; // Prints out 27

Now, ->acts like the deference symbol. It will dereference the pointer and then use the member functions and variables as if you had used .with a non-pointer object. Even with an object that is not a pointer you can use the ->by first getting the address:

现在,->就像尊重符号一样。它将取消引用指针,然后像使用.非指针对象一样使用成员函数和变量。即使对象不是指针,您也可以->通过首先获取地址来使用:

CObj object;
(&object)->MemberFunction();

That's just a brief overview of pointers, hope it helps.

这只是指针的简要概述,希望它有所帮助。

回答by Bo Persson

You can use the "normal" .to access the objects members, but you have to dereference the pointer first.

您可以使用“普通”.来访问对象成员,但您必须先取消对指针的引用。

Due to operator precedence, this will look like (*obj).member. For those who think this is too much to write, obj->memberis a shorter alternative.

由于运算符优先级,这看起来像(*obj).member。对于那些认为这样写太多的人来说,这obj->member是一个较短的选择。

If you have an object cof type Class, *c.ptrmeans dereferencing a pointer ptrthat is a member of Class. That is one reason for (*obj).member, which means something else.

如果您有一个c类型为 的对象Class,则*c.ptr意味着取消引用作为ptr的成员的指针Class。这是 的一个原因(*obj).member,这意味着别的东西。

回答by Luchian Grigore

Actually, you're wrong. You do:

其实,你错了。你做:

obj=new Class();

or

或者

Class *obj = new Class;

which are completely different.

这是完全不同的。

Class *obj;
*obj = new Class;

wouldn't compile.

不会编译。

objis of type Class*, so that's what you can assign to it (and what new Classreturns).

obj是 type Class*,所以这就是您可以分配给它的内容(以及new Class返回的内容)。

回答by Vikas Rao

More precisely u can do like this

更准确地说,你可以这样做

Class  obj;
Class* obj_ptr;

obj_ptr = &obj;

// Now onwards you can use the pointer to access data members and function

obj_ptr->a = 10; // Like this