0

Assume the following:

class child : public parent
{
  public:
  fun1(parent * obj);

  //somewhere on the child class:
  fun2 ()
  {
    fun1(this::...POINTER TO THE PARENT....); //how can I do such a thing without having to create an object of parent class?
  }

};

I am looking for something similar to 'this' pointer that points at the address of the current class. But, is there a "this" kinda thing for referencing a parent class inside a child's class?

gggs
  • 1
  • 1
  • 1
    Your example is passing an object, not a pointer. And `this` is already implicitly convertible. – StoryTeller - Unslander Monica Nov 27 '17 at 13:33
  • 1
    You cannot point to the parent object because there isn't a different object, the child object is also a type of parent, so that pointer would be `this`. In other words you'd just say `fun1(this)` – Cory Kramer Nov 27 '17 at 13:34
  • @StoryTeller good catch, I fixed that part. Thanks – gggs Nov 27 '17 at 13:34
  • 2
    Yeah, it's still strange. Why are you passing a reference to a pointer? It's highly suspect. – StoryTeller - Unslander Monica Nov 27 '17 at 13:35
  • 1
    A class is just a definition, it doesn't use memory in the program. `this` is a pointer to the current object, not to its class. And the current object is an object of its class but it is also an object of its parent class and of all parent classes of the parent class, recursively. That *"something"* similar to `this` you are looking for is `this` itself. – axiac Nov 27 '17 at 13:37
  • The things is this pointer needs to be modified, and passing it as a pointer won't allow any changes. That's why I am using pass by reference *&. Is this possible? – gggs Nov 27 '17 at 13:39
  • Relevant: "dynamic_cast from scratch" by Arthur O'Dwyer https://www.youtube.com/watch?v=QzJL-8WbpuU – NaCl Nov 27 '17 at 13:39
  • Post the definition of `fun1()` – axiac Nov 27 '17 at 13:40
  • Smelling an [XY Problem](http://xyproblem.info/) here. What are you _actually_ trying to achieve? – Jabberwocky Nov 27 '17 at 13:40
  • @MichaelWalz or it's a misunderstanding that would be solved by a book (link for OP's reference) https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – UKMonkey Nov 27 '17 at 13:46

1 Answers1

2

The parent is the base class not the derived class. Also this is implicitly convertible to the base class type, so you could just pass it around.

In your case:

class child : public parent
{
  public:
  fun1(parent * obj);

  //somewhere on the child class:
  fun2 ()
  {
    fun1(this);
  }

};

And finally, in the specific case you are showing, what you are trying to do doesn't make any sense. The child can access directly to any protected or public member of the base class, so you don't need to pass a pointer to the parent.

Like the following:

class parent
{
    /* Rest of the code here */
protected:
    int m_member;
};

class child : public parent
{
public:
    int fun1() { m_member = 1; } 
};
OriBS
  • 722
  • 5
  • 9
  • But that doesn't even compile for me! When I try to deference a protected member of the parent class through an object of the parent, it doesn't compile. – gggs Nov 27 '17 at 14:10
  • Sorry, didn't try to compile it, and forget to add a return type to fun1. It should work now. – OriBS Nov 27 '17 at 14:16
  • so, if inside fun1(), I allocated memory for a new parent object like this: parent * p = new parent; and when I try to deference p (p->m_member = 1) I get an error! – gggs Nov 27 '17 at 14:24