How to make a polymorphic pointer with the this pointer in the parameter?

Asked

Viewed 346 times

2

For example, in Qt (correct me, if the logic is wrong, I haven’t touched Qt in a while), you can do this:

QLabel label = new QLabel(this);

Now let’s suppose:

#include <iostream>
class AbstractBase
{
    public:
        virtual void A() = 0;
};

class DerivedClass : public AbstractBase
{
public:
    void A()
    {
        std::cout << "ClassA";
    }
    DerivedClass(AbstractBase* Base)
    {
        A();
    }
};
int main()
{
    AbstractBase* A = new DerivedClass(this);
}

But the compiler returns: "invalid use of this in non-member Function". What is the correct way to add the 'this' parameter in this case? (AbstactClass*, why it is derived. ) This is possible (reference A as the parameter)?

  • The error stated is why you are using this outside the context of a class. You will need a variable of the type AbstractBase* there. Other than that, you’re calling a virtual method in a constructor, which doesn’t work in C++.

  • Is this just a test of yours or part of a larger program? What do you really want to do?

  • @C.E.Gesser The example is representative;

  • Okay, but I think we need more inputs. You don’t use the parameter Base, for example, so it is complicated to suggest something without knowing what it will be used for.

  • @C.E.Gesser See the editions.

  • I made a mistake asking; I will delete the question.

  • Um... I still don’t see your need to pass the parameter in this case. You want to do something like Qt, where you pass an object that will be the owner of the newly constituted object?

  • @C.E.Gesser Qt is just one example.

  • It’s that you can’t use this where are you using, because this means "a pointer to the object under which the current method is being invoked", and you are in the function main, which is a free function, is not a member of any object.

Show 4 more comments

3 answers

2

The expression this results in a pointer to the object in which you are calling the member function at the moment. It makes no sense to write this out of a member function since there is no object in this case and the compiler will complain.

If you want a pointer to the current object within the constructor, simply type this. You don’t need to read it like a parameter. The kind of this will be the class in which you write the expression, but you can convert to a base class smoothly.

DerivedClass()
{
    DerivedClass* self = this;
    AbstractBase* base = (AbstractBase*)this;
}

In the case of the Qt example you wrote, this line should appear within a class. The goal is to pass to the QLabel who is your relative.

Another problem:

AbstractBase* obj = new DerivedClass(obj);

This is valid. But note that the constructor will be run before that the variable obj receive the object. Then the value passed as argument here is junk. The Clang gives the following warning:

Warning: variable 'obj' is uninitialized when used Within its Own initialization [-Wuninitialized]

1

I’m not sure what the purpose of the code is, but you’re using the keyword this outside the context of a class method, that’s what’s wrong.

this a pointer to the current context class, as you are within a function and not a method, there is no valid class context for you at this time, hence the error generated by the compiler.

  • I clarified the question.

  • Luke. There is no possibility of you using the keyword this within a function! This is a difference between C++ and Java, C++ has elements of structured programming as functions while Java only has methods.

0


The example of QLabel with the parameter this makes sense if the code is being executed in the context of a class that may contain a QLabel, an instance of QWidget, since the first parameter would be the element "parent" (Parent) label.

However, outside a class that implements QWidget, the code won’t work.

In your case, I believe you need only create the class, without the this as an argument:

 AbstractBase* A = new DerivedClass;
  • I clarified the end of the question.

  • @Lucashenrique I edited the answer. I don’t think there is an instantiation with this unless you want to pass the reference of the class where the code is running to the constructor of the object being instantiated.

  • Thank you :). That’s right. I wanted to, too, show the relative of the object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.