5 Replies - 388 Views - Last Post: 09 May 2019 - 11:43 AM Rate Topic: -----

#1 Neonz27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 08-May 16

When and why should I use a shared pointer over a double pointer?

Posted 08 May 2019 - 08:39 PM

[DELETED]

This post has been edited by Neonz27: 08 May 2019 - 08:39 PM

Is This A Good Question/Topic? 0
  • +

Replies To: When and why should I use a shared pointer over a double pointer?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15063
  • View blog
  • Posts: 60,150
  • Joined: 12-June 08

Re: When and why should I use a shared pointer over a double pointer?

Posted 08 May 2019 - 08:57 PM

Say what?

Was there a solution?
Was This Post Helpful? 0
  • +
  • -

#3 Neonz27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 08-May 16

Re: When and why should I use a shared pointer over a double pointer?

Posted 08 May 2019 - 09:16 PM

My primary question is why would I use a shared_ptr type over a **ptr type to return a pointer that point's to another class's pointer which points to the instance of an object. What overhead would it add and what would be a reason to prefer them? Consider the following (keep in mind this is psuedo-code):

Accessing Through Double Pointer
#pragma once
#include <iostream>

class Type1 {
public:
  Type1(Type2 &type2) : objPtr(&type2) {
    std::cout << "Type1 Object Created!" << std::endl;
    std::cout << "Type2 ObjectPtr Created!" << std::endl;
  }

  ~Type1() {
    delete objPtr;
    objPtr = nullptr;

    std::cout << "Type1 Object Destroyed!" << std::endl;
    std::cout << "Type2 ObjectPtr Destroyed!" << std::endl;
  }

  Type2 **getObjPtr() {
    return &objPtr;
  }

private:
  Type2 *objPtr;
};



Compared To

Accessing Through Shared Pointer
#pragma once
#include <iostream>
#include <memory>

class Type1 {
public:
  Type1(Type2 type2) : objPtr(type2) {
    std::cout << "Type1 Object Created!" << std::endl;
  }

  std::shared_ptr<Type2> getObjPtr() {
    return objPtr.get();
  }
  
private:
  std::shared_ptr<Type2> objPtr;
};


Was This Post Helpful? 0
  • +
  • -

#4 Neonz27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 08-May 16

Re: When and why should I use a shared pointer over a double pointer?

Posted 08 May 2019 - 09:18 PM

No, actually I made a mistake and posted the half-written question and couldn't figure out how to delete it. The new post is here.

View Postmodi123_1, on 08 May 2019 - 09:57 PM, said:

Say what?

Was there a solution?

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6914
  • View blog
  • Posts: 23,508
  • Joined: 05-May 12

Re: When and why should I use a shared pointer over a double pointer?

Posted 09 May 2019 - 03:47 AM

Merged topics...
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6914
  • View blog
  • Posts: 23,508
  • Joined: 05-May 12

Re: When and why should I use a shared pointer over a double pointer?

Posted 09 May 2019 - 11:43 AM

In general, you don't want to give out pointers to the internals of your class. So that means that your first Type1 is not a good practice. The reason for this is because somebody can change the state of your object without you knowing about it. Furthermore, you now have an object lifetime problem. Can you safely delete an instance of your object while other objects maybe pointing to a piece of data that is in your object?

So in your second Type1 class, now you won't have any lifetime issues because both your object as well as any other objects that you share the Type2 instance with will keep the Type2 object alive for as long as anybody has a reference to it. Since the Type2 object is also outside your class, you cannot have any expectations of state changes within the other object.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1