简体   繁体   English

C ++ :. cpp文件中的实例本身的表示关键字定义了类的功能

[英]C++: Representation keyword of the instance itself inside a .cpp file defining functions of a class

For example, I create a class named Circle and define it in Circle.h . 例如,我创建一个名为Circle的类,并在Circle.h对其进行Circle.h

class Circle {
    public:
        Circle *parent;

        Circle();
};

And in a separate Circle.cpp file under the same directory as Circle.h , I define the constructor. 并在一个单独Circle.cpp同一个目录下的文件Circle.h ,我定义构造函数。

Circle::Circle() {
    // creates a root circle, parent is set to itself
    *parent = ????
}

What should I fill in the ???? 我应该填写什么???? part? 部分? In AS3, I know you can use the this keyword to represent the instance itself when defining functions of the class. 在AS3中,我知道在定义类的函数时可以使用this关键字表示实例本身。 What do you use in C++? 在C ++中使用什么?

EDIT 2012/11/29 23:56 编辑2012/11/29 23:56

There is another constructor: 还有另一个构造函数:

Circle(Circle*);

Circle::Circle(Circle *cParent) {
    *parent = *cParent;
}

And a function, when called, creates a new Circle instance and set the new instance's *parent to caller. 一个函数在被调用时会创建一个新的Circle实例,并将新实例的*parent为调用者。

void addChild();

void Circle::addChild() {
    Circle child(????);
}

???? is still that mystery part. 仍然是那个神秘的部分。 According to resources I have read, this seems to be deprecated or has become of other meanings? 根据我阅读的资源, this似乎已被弃用或具有其他含义?

The keyword you're looking for is this ; 您要寻找的关键字是this it's a pointer to the instance on which the function was invoked. 它是在其上调用该函数的实例的指针。 In your example, you'd use it like this: 在您的示例中,您将像这样使用它:

Circle::Circle()
{
  parent = this;
}

Note that it must be parent = this; 注意它必须是parent = this; , not *parent = this; 而不是 *parent = this; . The latter would assign into the object parent is pointing to, which is not valid ( parent has an indeterminate value). 后者将分配给parent对象指向的对象,这是无效的( parent具有不确定的值)。 Fortunately, it wouldn't compile, as you'd be assigning a pointer into an object. 幸运的是,它不会编译,因为您将向对象分配一个指针。

For cases such as this (initialising a data member inside a constructor), C++ has the initialiser syntax, which is generally preferred to assigning the members in the constructor body. 对于此类情况(在构造函数内部初始化数据成员),C ++具有初始化程序语法,通常比在构造函数主体中分配成员更可取。 The code would then look like this: 代码如下所示:

Circle::Circle()
  : parent(this)
{}

Likewise, in C++ the this keyword is a pointer to the current object. 同样,在C ++中, this关键字是指向当前对象的指针。 Since it's a pointer, you should be assigning to the parent pointer directly instead of dereferencing it. 由于它是一个指针,因此您应该直接分配给parent指针,而不要取消对其的引用。

parent = this;

However, are you sure you want the parent of your circle to be itself? 但是,您确定要让圈子的父母成为自己吗? Often, the top-most object in a hierarchy has a null parent pointer: 通常,层次结构中最顶层的对象具有空的父指针:

parent = nullptr;

Or in a compiler that doesn't support nullptr , use NULL or 0 . 或者在不支持nullptr的编译器中,使用NULL0

Since you didn't initialise your parent pointer to point at any valid Circle object, your code invokes undefined behaviour when you dereference it. 由于您没有初始化parent指针以指向任何有效的Circle对象,因此在取消引用时,代码将调用未定义的行为。 You are then attempting to assign an object of type Circle* to an object of type Circle , which your compiler will complain about. 然后,您试图类型的对象赋给Circle*到类型的对象Circle ,你的编译器会抱怨。

According to resources I have read, this seems to be deprecated or has become of other meanings? 根据我阅读的资源,这似乎已被弃用或具有其他含义?

You're either reading bad resources or misreading them. 您正在阅读不良资源或误读它们。 The this pointer is alive and well in C++(03|11). 在C ++(03 | 11)中, this指针仍然有效。

Moving backwards, your mystery's solution is to use this : 向后移动,您的难题的解决方案是使用this

void Circle::addChild() {
    Circle child(this);
}

This code is almost right: 这段代码几乎是正确的:

Circle::Circle(Circle *cParent) {
    *parent = *cParent;
}

But not quite right becuase *parent dereferences parent , which isn't what you want to do. 但是不是很正确,因为*parent取消了parent ,这不是您想要的。 What you want to do is assign the parent pointer, not make changes to the thing that parent points to: 你想要做的是指定parent指针,不做出改变的东西parent点:

Circle::Circle(Circle *cParent) {
    parent = cParent;
}

Simalarly with the ctor: 与ctor同时:

Circle::Circle() {
    // creates a root circle, parent is set to itself
    parent = ????
}

However in this case, since a root node has no parent conceptually, the parent member of that node shouldn't point to itself, but to nothing: 但是,在这种情况下,由于根节点在概念上没有父节点,因此该节点的parent成员不应指向自身,而不能指向任何节点:

Circle::Circle() {
    // creates a root circle, parent is set to itself
    parent = NULL; // or '0' or 'nullptr' with C++11
}

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

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