Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:16:31+00:00 2026-05-31T09:16:31+00:00

I’m a bit confused with these C++ pointers and I would like to know

  • 0

I’m a bit confused with these C++ pointers and I would like to know if it’s possible to create a pointer inside a constructor that will point to the instance of the class (something on the lines of the following).

class Room{
public:  
    Room();  
    ~Room();  
private:  
    Room* ptrToSelf;  
};  

Room::Room(){  
    ptrToSelf = &(what should I write in here to have the pointer point to it)  
}

I know this might sound crazy (newbie here), but I just wanted to know if it is possible. Could someone please explain this to me.
Many thanks

Edit:
Thank you everyone for answering me so fast. I was asking because I wanted to use a public Room*::getPointer(); method to return the pointer for the class. I have a character that moves from room to room and I’m using pointers to achieve this. I’m getting the input from the user in form of string and I can’t pass that string to the function to character.moveTo(const Room*) method I created. I need a way to “convert” the string into a pointer for the class so that I could pass the pointer to the method.

Edit:
I was thinking of implementing a method that will return the pointer for the object based on a string type parameter:

Room::Room* getPointer(const string &nameOfRoom){  
    //I can't use this return statement as the following error  
    //Error: 'this' may only be used inside a nonstatic member function  
    return this; 

Edit (hopefully last one):
Is this returning what I’m looking for?

Room::Room* getPointer(const string &nameOfRoom){    
    return &Room();  
}

or it’s returning the address of a new Room?
Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:16:32+00:00Added an answer on May 31, 2026 at 9:16 am

    Just use the implicit this parameter:

    ptrToSelf = this;
    

    Or, even better (ignoring the fact that ptrToSelf is a rather silly idea to begin with):

    Room::Room() : ptrToSelf(this) { }
    

    EDIT: @BenjaminLindley drew my attention to the fact that there will be issues with certain other member functions. In particular, the copy constructor won’t behave correctly by default (ptrToSelf will simply be copied from the source instance, so both instances will end up pointing to the one). You may therefore need to provide a copy constructor that ensures ptrToSelf is assigned correctly. The base case is this:

    Room::Room(const Room& room) : ptrToSelf(this) { … }
    

    You also need to worry about the assignment operator, but as long as it simply invokes the copy constructor, you’ll be fine. The following general-purpose implementation will do the trick:

    Room& operator=(Room room) { swap(room); return *this; }
    void swap(Room& room) { /* Swap rooms… */ }
    

    (P.S.: It should be class Room {…, not class Room(){….)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.