简体   繁体   English

如何使用带有*变量名的Struct?

[英]How to use Struct with a *variable name?

private:
 // data containing character strings
 // shared by Strings when copying/assigning
 struct SharedData
 {
   char *data; // 0-terminated char array
   size_t n;      // number of non-0 characters in string
   size_t count;  // reference count, how many Strings share this object?
 };

 SharedData *shared; //here i don't understand how to use.

How do I use n? 如何使用n?

do I just do this 我只是这样做吗

shared -> n = 3;

I keep getting segmentation faults so i don't know what i am doing. 我不断出现细分错误,所以我不知道自己在做什么。 I want to grab the length of a string and then pass it to n to be stored 我想获取一个字符串的长度,然后将其传递给n进行存储

Your class contains a pointer to SharedData. 您的类包含一个指向 SharedData的指针 You need to make that pointer point to something, presumably by doing this 您需要使指针指向某物,大概是通过这样做

shared = new SharedData();
shared->n = 3;
// etc.

Same thing is true of data inside shared 共享内部的数据也是如此

shared->data = new char[100];
strcpy(shared->data, ...);

I have a feeling this is going to be quite a hard exercise for you if you don't have a good grasp of pointers yet. 如果您还没有很好地掌握指针,我感觉这对您来说将是一项艰巨的练习。

You should allocate some memory first: 您应该先分配一些内存:

SharedData *shared = new SharedData;
shared->n = 3;

Better use something like this: 最好使用这样的东西:

struct SharedData {
    SharedData(size_t Capacity) : data(0), capacity(Capacity), length(0), count(1) {
        data = new char[capacity];
    }
    ~SharedData() {
        if (--count <= 0) delete [] data;
    }
    char *data; // 0-terminated char array
    size_t length;      // number of non-0 characters in string
    size_t capacity;
    size_t count;  // reference count, how many Strings share this object?
};
SharedData* shared = new SharedData(1000);

Edit: Of course this short example lacks of copy constructor and handle reference counting in appropriate manner. 编辑:当然,这个简短的示例缺少复制构造函数,并且无法以适当的方式处理引用计数。 But this is not subject of this answer. 但这不是此答案的主题。

Modifying an un-allocated point in memory is undefined behavior. 修改内存中的未分配点是未定义的行为。

You must allocate memory for shared or set it to a valid address. 您必须为shared分配内存或将其设置为有效地址。

class Class
{
private:

  SharedData *shared;

  ...

public:

  Class() : shared(new SharedData()) {}

  ~Class() { delete shared; }

  void doSomething() { shared->n = 3; }

};

Try not to use bare pointers when you can use smart pointers: 当可以使用智能指针时,请尽量不要使用裸指针:

class Class
{
private:

  std::unique_ptr<SharedData> shared;

  or

  std::shared_ptr<SharedData> shared;

  ...

public:

  Class() : shared(new SharedData()) {}

  void doSomething() { shared->n = 3; }

};

Are you using a C++11 compiler? 您正在使用C ++ 11编译器吗? Why don't you make it a bit easier? 您为什么不轻松一点?

struct SharedData
{
   std::string data;
   size_t count;  // reference count, how many Strings share this object?
};

std::shared_ptr<SharedData> shared = std::make_shared<SharedData>();

If you don't need the actual count of the number of referrers, you just want to know if there is anyone referencing your data, simply use a std::shared_ptr<std::string> . 如果您不需要实际的引荐来源人数计数,您只想知道是否有人在引用您的数据,只需使用std::shared_ptr<std::string> It will keep track of references and destroy the string object when all the references are gone. 当所有引用消失时,它将跟踪引用并销毁字符串对象。

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

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