简体   繁体   English

带指针和构造函数的typedef结构

[英]typedef struct with pointer and constructor

I am writing a struct _Point3d and typedefed it to Point3d and provided a pointer declaration PPoint3d next to Point3d (Please see code). 我正在编写struct _Point3d并将其类型定义为Point3d ,并在PPoint3d旁边提供了一个指针声明Point3d (请参见代码)。 There is a constructor which initializes members of the struct . 有一个构造函数初始化struct成员。 I have twofold queries here. 我在这里有双重疑问。

typedef struct _Point3d
{
    float x;
    float y;
    float z;

    _Point3d(float _x, float _y, float _z) : x(_x), y(_y), z(_z)
    {

    }

}Point3d, *PPoint3d;

One: What does this convention of providing pointer after struct name (typedef) really means? 一:在结构名称(typedef)之后提供指针的约定的真正含义是什么? provided we can create the pointer of Point3d like this 只要我们可以像这样创建Point3d的指针

Point3d* _ppoint2 = new Point3d(1.0, 0.0, 0.0);

Two: Is there any difference in creating instances of Point3d in these two different ways. 二:用这两种不同的方式创建Point3d实例是否有任何区别。

PPoint3d _ppoint1 = new Point3d(0.0, 1.0, 0.0);
Point3d* _ppoint2 = new Point3d(1.0, 0.0, 0.0);

I am writing a struct _Point3d and typedefed it to Point3d and provided a pointer declaration *PPoint3d next to Point3d 我正在编写一个_Point3d结构并将其类型定义为Point3d,并在Point3d旁边提供了一个指针声明* PPoint3d

Stop right there, Kiddo. 就在那儿停下来 This is 2016, not 1997. No need to follow arcane microsoft practice any more. 这是2016年,而不是1997年。不再需要遵循Microsoft神秘的做法。

Do this instead: 改为这样做:

struct Point3d
{
    float x;
    float y;
    float z;

    Point3d(float _x, float _y, float _z) : x(_x), y(_y), z(_z)
    {

    }
};

using PPoint3d = std::unique_ptr<Point3d>;
using SharedPoint3d = std::shared_ptr<Point3d>;

Raw pointers are to be used in low-level implementations. 原始指针将在低级实现中使用。 When used as part of an interface they make your code susceptible to being silently hijacked by your user's unintended and un-noticed mistakes. 当用作界面的一部分时,它们使您的代码易于被用户的意料之外的错误所劫持。

For this reason they are evil when used as part of an interface. 因此,当它们用作接口的一部分时,它们是邪恶的。 So evil that it is evil to even hint to users of your class that they should use them. 如此邪恶,以至于甚至暗示类的用户他们应该使用它们是邪恶的。

First, you are in C++, so you don't need to typedef your structs. 首先,你是在C ++中,因此你不需要typedef的结构。 You can write this. 你可以写这个。

struct Point3d {
//...
};

The typedef idiom is the norm in C, but not in C++ (and since you use new you clearly are in C++, not in C). typedef习惯用法是C语言中的规范,但不是C ++中的规范(并且由于使用new您显然是在C ++中,而不是C语言中)。

Secondly, there is no difference, as a typedef really is a type alias. 其次,没有区别,因为typedef实际上是类型别名。

There are differences, but effectively the same thing is happening. 存在差异,但实际上同一件事正在发生。

In

 Point3d* _ppoint2 = new Point3d(....);

You are taking the address of the new Point3d and storing it in the L-value (left hand side) called _ppoint2. 您正在获取新Point3d的地址,并将其存储在称为_ppoint2的L值(左侧)中。 You are doing the same with 你在做同样的事情

 PPoint3d _ppoint1 = new Point3d(....);

The only difference is how you are writing the L-value. 唯一的区别是您如何编写L值。

_ppoint2 is a struct type, but you are annotating it with an asterisk to make it a pointer type. _ppoint2是一种结构类型,但是您要在其上加上星号以使其成为指针类型。 With _ppoint1 you have "baked in" the pointer to the struct. 使用_ppoint1,您可以“烘焙”指向该结构的指针。

Functionally they are identical except in type. 在功能上,除了类型外,它们是相同的。 This difference in type means that the manner in which you use them will differ, because that manner must be type-aware. 这种类型上的差异意味着您使用它们的方式将有所不同,因为这种方式必须能够识别类型。 For example, one does not need to include the asterisk to get the pointer value from _ppoint1. 例如,不需要包含星号即可从_ppoint1获取指针值。

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

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