简体   繁体   中英

C++ | temporary pointers lifetime

I'm studying C++ and in all my "experiments" i'm trying to understand temporary objects (rvalues) lifetime.

My question is:

Having an object which contains a const char* pointer, what happens when i want to use a constructor which takes "const char*" as argument? Usually temporary objects get destructed automatically but what happens with pointers created in this way?

I'm not using std::string or other c++11 classes for now because I'm just focusing on understanding rvalueness.

An example below:

class MyAwesomeClass {

private:
    const char* data;

public:
    MyAwesomeClass(const char* ptr) {

        this->data = ptr;
    }

    MyAwesomeClass(MyAwesomeClass&& myAwesomeClassRVALUE) {

        this->data = myAwesomeClassRVALUE.data;
        myAwesomeClassRVALUE.data = nullptr;
    }

    ~MyAwesomeClass() {

        delete data;
    }
};

int main() {

    MyAwesomeClass s = "My Awesome Class' string data.";

    return 0;
}

Usually temporary objects get destructed automatically but what happens with pointers created in this way?

Pointers are objects themselves. Same thing happens to temporary pointer objects as happens to all other temporary objects.

Your example deletes a pointer pointing to a string literal, so the behaviour of the program is undefined. The class would also have a broken assignement operators even if it was used correctly by passing pointer to an array allocated with new[] .

As for rvalueness, the only rvalue expression in the example that I could find is nullptr .

Note this answer is based on a previous edit of the question where MyAwesomeClass was a String class.

Your String class is not really a string class as it doesn't own the underlying string data. It's more akin to std::string_view .

You have two avenues I can see you can pursue:

  1. Your class owns the underlying data. In this case the class is a wrapper around std::string and has std::string data member. Look up composition. No need to worry about lifetime as data member. Look up composition. No need to worry about lifetime as data member. Look up composition. No need to worry about lifetime as std::string` is well behaved.

  2. Your class is like a "pointer/reference/view" to another string. In this case you have a const char* and maybe std::size_t size data member. You basically have a std::string_view (except for the wisdom, expertise and experience that went into designing std::string_view ). Since you don't own the underlying data you can't do anything about the lifetime of the underlying data. The user of the class must make sure it doesn't end with a "YourStringView" to an expired object, just as he/she needs to make sure it doesn't end up with a reference/pointer to an expired object.

The semantics of these two scenarios are wildly different (as the difference between an object and a pointer to an object).


Anyway I wouldn't recommend you do any of this except for maybe learning reasons. std::string_view already exists so just use that. If you want the printing capabilities use the fmt library or the C++ format library (that is based on the mentioned fmt library).

Even if you decide to do this for learning purposes I highly encourage you look into these alternatives and learn from how they are doing things.


 MyAwesomeClass(const char* ptr) { this->data = ptr; } ~MyAwesomeClass() { delete data; }

Oh no, no, no! No!!

Please look into RAII and rule of 0/3/5 . Your class either owns the pointed object or it doesn't. If it owns it then it is responsible for creating it and deleting it. If it doesn't then it can't do either. You can't have "half of responsibilities" where you are responsible for deleting it but not for creating it.

In user code you should never need to manually manage memory. Use the rule of 0.

Quick answer is that your class does not own the data, but just the raw pointer. Under certain conditions you will see a problem with delete operator. Raw pointer are not great tool to ensure correct object ownership.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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