'this'指针,使用'this'指针继承子类中超类的函数

嗨,我想了解如何使用'this'指针。现在我编写了一个示例程序,它使用类Image,它是类BMP的子类。现在,函数TellWidth和TellHeight在BMP类中声明。现在编译器给出了一个错误,表示在Image中不存在TellWidth函数。但由于Image是BMP的子类,因此它不能继承BMP中的函数。 我该如何解决这个问题
void Image :: invertcolors()
{
    int x;
    int y;

    int width  =(*this).TellWidth();
    int height = (*this)->TellHeight();

    for(x=0,x<=height-1;x++){
        for(y=0,y<=width-1;y++){
            (*this)(x,y)->Red = (255 - (*this)(x,y)->Red);
            (*this)(x,y)->Blue = (255 - (*this)(x,y)->Blue);
            (*this)(x,y)->Green = (255 - (*this)(x,y)->Green);

        }
    }
    delete width;
    delete height;
}
图片
class Image : public BMP  
{
public:

    void invertcolors();

    void flipleft();
    void adjustbrightness(int r, int g, int b) ;

};
这个类太大了,不能在这里发布,这里是一个相关摘录
class BMP {
private:
   int Width;
   int Height;
public:
   int TellBitDepth(void) const;
   int TellWidth(void) const;
   int TellHeight(void) const;
};
    
已邀请:
TellWidth()
很可能在
BMP
类中声明为
private
(或没有访问者修饰符)。
Image
类需要
protected
public
才能访问它,如果你想在
Image
类中覆盖它,它也必须是
virtual
。 适当的
this
用法是这样的:
int width = this->TellWidth();
int height = this->TellHeight();
阅读
this
获取
this
的快速教程。     
关于
this
的一点:你很少需要明确提及它。通常的例外是当你需要将它传递给非成员函数时(这里似乎不是这种情况。) 当你在一个类成员函数中时,
this->field
可以简单地被访问为
field
,而
this->function(x)
可以被调用为
function(x)
。 以下是对您的代码的一些评论。我希望他们有所帮助。
void Image :: invertcolors()
{
    // Don't define these here; that's old C-style code. Declare them where
    // they're needed (in the loop: for (int x=0...)
    int x;
    int y;

    // Change the lines below to
    // int width  = TellWidth();
    // int height = TellHeight();
    //    (*this).TellWidth() should work, but is redundant;
    //    (*this)->TellHeight() should probably *not* work, as once you've
    //    dereferenced *this, you're dealing with an object instance, not a
    //    pointer. (There are ways to make (*this)->that() do something useful,
    //    but you're probably not trying something like that.)
    int width  =(*this).TellWidth();
    int height = (*this)->TellHeight();

    for(x=0,x<=height-1;x++){
        for(y=0,y<=width-1;y++){
            // After locating the BMP class through google (see Edit 2),
            // I've confirmed that (*this)(x,y) is invoking a (int,int) operator
            // on the BMP class. It wasn't obvious that this operator 
            // was defined; it would have been helpful if you'd posted
            // that part of the header file.
            (*this)(x,y)->Red = (255 - (*this)(x,y)->Red);
            (*this)(x,y)->Blue = (255 - (*this)(x,y)->Blue);
            (*this)(x,y)->Green = (255 - (*this)(x,y)->Green);

        }
    }
    // These are int values. They can't be deleted, nor do they need to be.
    // I'm sure the compiler has told you the same thing, though perhaps not
    // in the same way.
    delete width;
    delete height;
}
编辑:看起来有人和OP一样接受同样的课程。这里展示的例子让我更清楚的是Image应该有某种数组访问器,这可以解释
(*this)(x,y)->Red = (255 - (*this)(x,y)->Red)
的目的是什么。 编辑2:这是原始BMP类的来源。     
class Image定义为
class Image : public BMP  
{
public:

    void invertcolors();

    void flipleft();
    void adjustbrightness(int r, int g, int b) ;

};
    

要回复问题请先登录注册