Thread: make unique pointer equal to pointer or another unique pointer

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    make unique pointer equal to pointer or another unique pointer

    hi
    i cant use unique pointers i try searching but i didnt find anything usefull just this sites...
    C++11 FAQ
    and
    std::unique_ptr - cppreference.com
    !!
    so i have still a lot of questions!...
    std::unique_ptr<user_pass> conductor( root);//is it true? or it must be new root ?
    //user_pass is a link list and root is a pointer that point to the root of the list

    i want this unique pointer points to another pointer what should i do?
    for example
    conductor=root2;// doesnt work

    how about making this unique pointer equal to another unique pointer?
    i cant use sth like
    conductor=conductor->next;

    is sth like vector_of_pointers.push_back(*conductor); right??

    and also i need an example of using share pointers too!?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Can you please write a real question, not a bunch of gibberish with random question marks?!

    making this unique pointer equal to another unique pointer?
    Perhaps you misunderstand the meaning of the word "unique". It means "only one". If you could have more than one pointing to the same thing then they would be called shared pointers. But no, they're unique pointers, so you can only have one pointing to each thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point you are missing about unique_ptr is that there can be only one pointer to any one object at any given time. That is, you cannot have two or more unique ptrs pointing to the same object. If you manage that, then you violate the preconditions of the unique pointers.
    I can tell you that because there can be only one, a unique pointer is moveable. That means you can assign it an rvalue. You can also move another unique pointer into another:

    std::unique_ptr<T> a(...);
    std::unique_ptr<T> b(std::move(a)); // After this, a points to null.

    This will probably seem very weird to you and make no sense, so if you want to know more any why it won't work for you, you have to post an example.
    shared_ptr works they way you expect it to, because you can simultaneously have N pointers to the same object. The object will disappear when the last shared_ptr goes out of scope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    The point you are missing about unique_ptr is that there can be only one pointer to any one object at any given time. That is, you cannot have two or more unique ptrs pointing to the same object. If you manage that, then you violate the preconditions of the unique pointers.

    i dont want 2 pointer point to one thing!!
    i want to change the place it points to!
    it is impossible right?
    This will probably seem very weird to you and make no sense, so if you want to know more any why it won't work for you, you have to post an example.
    Code:
    user_pass *make_list(user_pass *root)
    {
        user_pass *conductor=root;
        char g;
        std::fstream User;
        std::fstream Password;
        User.open("Data/User_name.dat", std::fstream::in  |  std::fstream::app);//file's content ----->abc|ahhhhd|ds|dddssx|...
        Password.open("Data/Password.dat", std::fstream::in  |  std::fstream::app);//file's content ----->123|33d|423s|dd3sx|...
        root->next=0;
        int i=0;
        std::string tmp;
        while(!User.eof())
        {
            g=User.get();
            if(g!='|')
            {
            tmp.resize(i+1);
            tmp[i]=g;
            i++;
            }
            else
            {
              conductor->user=tmp;
              user_pass * u_new;
              tmp.resize(0); i=0;
              u_new=new user_pass;
              conductor->next=u_new;
              conductor=conductor->next;
              conductor->next=0;
            }
    
    
         }
        conductor=root;
        tmp.resize(0),i=0;
        while(!Password.eof())
         {
            g=Password.get();
            if(g!='|')
            {
            tmp.resize(i+1);
            tmp[i]=g;
            i++;
            }
            else
            {
              conductor->pass=tmp;
              tmp.resize(0); i=0;
              conductor=conductor->next;
            }
         }
         User.close();
         Password.close();
       //  delete  conductor;
        return root;
    }


    this is my code without unique pointer it works fine
    but you know this list and pointers must not remove before
    return node;

    if i want to delete the list i need to send a vector consist of root and conductor refrence and then save that in another variable in the function and at last pass it to a function that remove the list
    that seems nasty and i have to do it for 5 functions!!
    so is there any way to do this stuffs with share_ptr?
    std::unique_ptr<T> a(...);
    std::unique_ptr<T> b(std::move(a)); //
    After this, a points to null.
    This will probably seem very weird to you and make no sense

    no it just change the
    unique_ptr name right?
    what is the reson of doing this!!?
    Last edited by king_zart; 12-15-2012 at 03:59 PM. Reason: add some information to the code...

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You want the reset() method. as in: conductor.reset(root2). However, you need to make sure that the argument passed is indeed a newly allocated object, or otherwise an object that does not have something else controlling it's lifespan.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The shift key is about more than just exclamation marks and question marks; It's also how you create capital letters.
    Right now I care about helping you as much as you do about the professional quality of your posts. I've seen plenty of your posts, and you don't even try. The word "I" is always spelt with a capital letter.
    I suggest that you reprioritise and learn proper grammar etc before you learn programming.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. my code breaks unique pointer rule?
    By George2 in forum C++ Programming
    Replies: 19
    Last Post: 02-17-2008, 03:26 AM
  4. unique pointer issue
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 02-14-2008, 11:25 PM
  5. Replies: 2
    Last Post: 10-12-2002, 07:44 PM