Thread: Accessing functions of a unique_ptr

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18

    Accessing functions of a unique_ptr

    Hi guys,

    I might be doing this completely the wrong way but I'm trying to access functions contained by a unique_ptr.

    Here's what I'm trying to do:

    Code:
    while(mp_renderManager->getWindow()->close()) {
    ...
    }
    Now I'm able to access getWindow() but, I cannot however get to close().

    in my RenderWindow class it's as follows:

    Code:
    std::unique_ptr<sf::RenderWindow> mp_window;
    
    ...
    
    const std::unique_ptr<sf::RenderWindow> const { return mp_renderWindow; }
    How can I access the functions of sf::RendreWindow when it's wrapped in a unique_ptr?

    If this isn't possible, can I do it with a shared_ptr or something?

    Thanks.

    Jamie.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Does getWindow return a pointer?

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Assuming mp_window and mp_renderWindow are supposed to be the same and the last bit of code is the getWindow function with the name missing (you should read your question carefully to see that it actually makes sense!), then you should be returning a reference since you're not allowed to copy a unique_ptr.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    yeah, I made a mistake, it's supposed to be:

    Code:
    const std::unique_ptr<sf::RenderWindow> getWindow() const { return mp_window; }

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So, what's your actual error message from the compiler say?

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    So did changing it to this solve your problem or what?
    Code:
    const std::unique_ptr<sf::RenderWindow> & getWindow() const { return mp_window; }


    Usually people tell us the error message instead of making a guessing game out of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with accessing DLL functions
    By CodeRed in forum C Programming
    Replies: 17
    Last Post: 11-26-2012, 03:46 PM
  2. Accessing Structs in Functions Problem
    By opeth in forum C Programming
    Replies: 8
    Last Post: 04-02-2012, 09:11 PM
  3. std::unique_ptr
    By KIBO in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2010, 07:48 AM
  4. Accessing member functions of sockaddr_in
    By Stack Overflow in forum Linux Programming
    Replies: 8
    Last Post: 01-29-2005, 11:26 AM
  5. Replies: 2
    Last Post: 11-28-2003, 09:23 AM

Tags for this Thread