简体   繁体   English

指向指针 C++ 的指针

[英]Pointer to pointer C++

I can't really understand what pointer to pointer is in C++.我真的无法理解 C++ 中指向指针的指针是什么。 Let's say I have a class defined as follows:假设我有一个定义如下的类:

class Vector3 
{
   public:
         float x,y,z;
         //some constructors and methods
}

Now what if I have something like现在如果我有类似的东西怎么办

Vector3 **myVector3;

Is this SOMEHOW the C#'s equivalent of saying List<List<Vector3> myVector3 ?这在某种程度上 C# 相当于说List<List<Vector3> myVector3吗? And anyway, how can I dynamically allocate this myVector3 object?无论如何,我如何动态分配这个 myVector3 对象? Thanks.谢谢。

Is this SOMEHOW the C#'s equivalent of saying List<List<Vector3> myVector3 ?这在某种程度上 C# 相当于说List<List<Vector3> myVector3吗?

No.不。

And anyway, how can I dynamically allocate this myVector3 object?无论如何,我如何动态分配这个myVector3对象?

I don't understand the question.我不明白这个问题。

I can't really understand what pointer to pointer is in C++.我真的无法理解 C++ 中指向指针的指针是什么。

Go back to first principals.回到第一任校长。 What is a variable?什么是变量? A variable is storage for a value of a particular type .变量是特定类型存储

What operations are available on variables?对变量有哪些可用的操作? They may be read from , written to , or their address may be taken .它们可以被读取写入或者它们的地址可以被获取

What is the result of the address-taking operator, & ?取地址运算符&的结果是什么? A pointer to the variable.指向变量的指针。

What is a pointer?什么是指针? A value that represents a variable .表示变量

What operations are available on pointer values?可以对指针值进行哪些操作? A pointer may be dereferenced using * .可以使用*取消引用指针。 Doing so produces a variable .这样做会产生一个变量 (There are other operations available on pointers but let's not worry about those.) (在指针上还有其他可用的操作,但我们不用担心这些。)

So let's sum up.所以让我们总结一下。

Foo foo; is a variable of type Foo .Foo类型的变量。 It can contain a Foo .它可以包含一个Foo

&foo is a pointer. &foo是一个指针。 It is a value.这是一个价值。 When dereferenced it produces the variable foo :当取消引用时,它会产生变量foo

Foo foo;
Foo *pfoo = &foo;
*pfoo = whatever; // same as foo = whatever

pfoo is a variable. pfoo是一个变量。 A variable may have its address taken:一个变量可能有它的地址:

Foo **ppfoo = &pfoo;
*ppfoo = null;  // Same as pfoo = null.  Not the same as foo = null.

So there you go.所以你去。 ppfoo is a variable. ppfoo是一个变量。 It contains a value.它包含一个值。 Its value is a pointer.它的值是一个指针。 When that pointer is dereferenced it produces a variable.当该指针被取消引用时,它会产生一个变量。 That variable contains a value.该变量包含一个值。 That value is a pointer.该值是一个指针。 When it is dereferenced it produces a variable.当它被取消引用时,它会产生一个变量。 That variable is of type Foo .该变量的类型为Foo

Make sure this is very clear in your mind.确保这在您的脑海中非常清楚。 When you get confused, go back to first principles .当你感到困惑时,回到首要原则 Pointers are values, they may be dereferenced, doing so produces a variable.指针是值,它们可能会被取消引用,这样做会产生一个变量。 Everything flows from that.一切都源于此。

The C++ equivalent of a List would be std::vector. List 的 C++ 等价物将是 std::vector。 Never mind that your class is called vector;别介意你的类被称为向量; this is a vector as in a dynamically expandable sequence of like-typed objects.这是一个向量,就像在类似类型对象的动态可扩展序列中一样。

If you want a list of lists of Vector3 in C++, you want如果你想要一个 C++ 中 Vector3 的列表,你想要

std::vector<std::vector<Vector3> myVectorVectorVictor;

And that allocates one too.这也分配了一个。 No pointers needed.不需要指针。

Vector3 **myVector3;

Is basically just a pointer pointing to another pointer.基本上只是一个指向另一个指针的指针。 Dereferencing it gives you a pointer to a Vector3 .取消引用它会给你一个指向Vector3的指针。

example :例子 :

#include <iostream>

class Vector3
{
public:
    float x, y, z;
    //some constructors and methods
};

int main()
{
    Vector3 **myVector3 = new Vector3*[50]; // allocate 50 Vector3 pointers and set myVector3 to the first, these are only pointers, pointing to nothing.
    //myVector3[0]->x; myVector3[0] is a Vector3 pointer, currently pointing to nothing dereferencing this will result in Undefined Behaviour
    myVector3[0] = new Vector3; // allocate a Vector3 on the heap and let pointer 1 ( of the 50 ) point to this newly allocated object.
    myVector3[0]->x = 5;
    std::cout << "myVector3[0]->x : " << myVector3[0]->x; // prints "myVector3[0]->x : 5"
    std::cin.get();
}

You can do something like this:你可以这样做:

Vector3 * pointer = new Vector3; // points to a instance of Vector3 on the heap
Vector3 * * pointerToPointer = & pointer; // points to pointer; we take the address of pointer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM