Deleting 1 copy of a shared_Ptr

Hi,how could I set a shared_Ptr to nullptr?
Concourse has a std::shared_ptr<Text> called text.
It also has a setter and getter.
This currently doesn't work.

The plan is to delete all copies of the textToDelete from the elements in the list, 1 at a time.

1
2
3
4
5
6
7
8
  std::shared_ptr<Concourse>& currentElement = concourseList[i];
  if (currentElement->getText() == textToDelete)
        {
            currentElement->getText().reset();
            currentElement->getText() = nullptr;
            currentElement->setNamed(false);
            std::cout<< "bud" << std::flush;
        }
Last edited on
This feels like it doesn't make much sense. The whole point of using smart pointers is so that you don't have to waste your time controlling the lifetime of objects. The smart pointer does it for you.

This currently doesn't work.

What does that mean? Does it crash? Does it refuse to compile? Does the object not get deleted when you expect it to? What does that mean?

The plan is to delete all copies of the textToDelete from the elements in the list, 1 at a time.

The elements in the list don't have any copies of the textToDelete. The elements in the list contain a smart pointer which points at some other object. That's why it's called a shared_pointer; you don't have copies of the object, you just point at a shared single instance.
Last edited on
I explained that poorly, so each currentElement should have a pointer to a text object, but I want to have each currentElement's to then point to nullptr i nthat method. So in the view of the currentElement it no longer points to the actual text but now to a nullptr, and it's like it's been deleted from the currentElement Object, but the actualy text should still exist if there is a pointer to it.
Is this string object actually used by anything else? Why do you a shared_pointer to the string object, rather than just having a string object as part of the Concourse object? What else is using that exact same string object?

currentElement->getText() == textToDelete
This seems odd. This suggests that getText() function returns an actual string?

But this:
currentElement->getText().reset();
suggests that getText() returns a shared_pointer?

Which is it? What kind of object does getText() return?

Show us the declaration of the Concourse class so we can see what that function actually returns.
Last edited on
There is also a list of text objects too. I plan to delete that after I've deleted all this pointers to it in each concourse objectI find that has it.

The sharedPtr text is a custom object, for storing a text at a location and also an area that is clickable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Element
{
private:

protected:
	ElementType elementType;
	int offsetX;
	int offsetY;
	int locationX;
	int locationY;

public:
	Element();
	Element(int currentX, int currentY);

	ElementType getElementType() const;
	void setElementType(const ElementType& newElementType);
    int getLocationX() const;
	void setLocationX(int locationX);
	int getLocationY() const;
	void setLocationY(int locationY);
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Text : public Element
{
private:
    int editableX;
    int editableY;
    int fontSize{ 12 };
    std::string readableText;

protected:


public:
    Text(ElementType newElementType, int newLocationX, int newLocationY, QString newReadableText);

    int getEditableX() const;
    void setEditableX(int newEditableX);
    int getEditableY() const;
    void setEditableY(int newEditableY);
    int getFontSize() const;
    void setFontSize(int newFontSize);
    std::string getReadableText() const;
    void setReadableText(std::string& newReadableText);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Element
{
private:

protected:
	ElementType elementType;
	int offsetX;
	int offsetY;
	int locationX;
	int locationY;

public:
	Element();
	Element(int currentX, int currentY);

	ElementType getElementType() const;
	void setElementType(const ElementType& newElementType);
    int getLocationX() const;
	void setLocationX(int locationX);
	int getLocationY() const;
	void setLocationY(int locationY);
};


But Concourse inherits from NamedElement which inherits from Element.
1
2
3
4
5
6
7
8
9
10
11
12
class Concourse : public NamedElement
{
private:


protected:


public:
    Concourse(ElementType newElementType, int newLocationX, int newLocationY);

};


Last edited on
There is no function getText in any of that code.
I just realised

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class NamedElement : public Element
{
private:


protected:
    bool named { false };
    std::shared_ptr<Text> text {nullptr};


public:
    NamedElement();
    bool getNamed() const;
    void setNamed(bool newNamed);

    std::shared_ptr<Text> getText();
    void setText(std::shared_ptr<Text> &newText);
};
`getText()' returns another pointer, can't use it to modify NamedElement::text
I guess that you need to use `setText()'

> void setText(std::shared_ptr<Text> &newText);
¿ah? ¿why does it ask for a non-const reference?

1
2
3
4
if (currentElement->getText() == textToDelete){ //comparing the memory address not content, ¿is that what you want?
   std::shared_ptr<Text> empty; //because of your weird interface
   currentElement->setText(empty);
}
I think that may just be a mistake with the const.
I think I want to compare content, but i think comparing content and address should lead to the same result in this case.
I guess what I'm looking for is for currentElement->setText(nullptr);
Your empty does that right?
Also what exactly does the reset() do with the shared_pointer? (My idea was that it stopped pointing to whatever it was pointing to.
Last edited on
> I think that may just be a mistake with the const.
that's quite a design decision, rtfm

> I think I want to compare content,
let us know when you decide yourself
to get the content you need to dereference the pointer
if (*currentElement->getText() == *textToDelete)
(¿what type is `textToDelete'?)

> but i think comparing content and address
> should lead to the same result in this case.
I think... should... ¿why so many doubts?
¿how are you creating `textToDelete'?

> I guess what I'm looking for is for currentElement->setText(nullptr);
> Your empty does that right?
yes, `empty' points to null

.reset() does reset the pointer, but getText() created another one
you never touched NamedElement::text but a copy of it
Topic archived. No new replies allowed.