Pergunta

Let's say I have this example template:

template<class T>
class Wrapper
{
virtual T* ReturnPtr() = 0;
};

And then I inherit from it:

class Buffer; //some class
class BufferWrapper : public Wrapper<Buffer>
{
virtual Buffer* ReturnPtr(); //<< (1.)
}
  1. Is this the way to do it properly? When I write it like this it gives me no intellisense errors, but once I write ReturnPtr() somewhere,it tells me "the object has type qualifiers that are not compatible with the member function".
  2. Does this mean it's impossible to use virtual methods like that?
Foi útil?

Solução

Intellisense shows that message when you are calling a non-const function on an object that is const. As we can see, ReturnPtr is non-const. There are generally two reasons you might see this message. The first is when you are trying to call ReturnPtr on a const object of type BufferWrapper:

const BufferWrapper bw;
bw.ReturnPtr(); // Can't call non-const member function on const object

The second is when your BufferWrapper object is a data member of a class and you are calling ReturnPtr on it from within a const member function of that class:

struct SomeClass
{
  BufferWrapper bw;

  void SomeClass::SomeFunc() const
  {
    bw.ReturnPtr(); // Cannot call non-const member function here
  }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top