【问题标题】:'this' pointer, inheriting functions of super class in subclass using 'this' pointer'this'指针,使用'this'指针在子类中继承超类的函数
【发布时间】:2011-01-28 02:13:41
【问题描述】:

您好,我想了解如何使用“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;
};

【问题讨论】:

  • 请发布头文件中声明类 Image 和 BMP 的部分。包括 class 声明本身,以及 invertcolorsTellWidthTellHeight 的声明。还包括用于表示位图数据的任何类型的数据声明(例如,数组)和/或名为 xy 的字段声明(如果它们碰巧存在)(请参阅下面的答案。)您可能可以删除头文件中的所有其他内容,至少现在是这样。
  • 请同时包含名为RedGreenBlue 的任何字段或方法的定义。
  • 如果我删除不相关的红色/绿色/蓝色部分并使用(*this)-&gt; 修复明显错误,它对我来说编译得很好。也许您遇到了链接问题,而不是编译问题?也许这些函数要么没有定义,要么它们的单元没有包含在链接过程中?
  • 另外,这不太可能,但也许您正在使用一些不喜欢这些函数的(void) 形式参数列表的奇怪编译器?它完全有效,但在 C++ 中很少使用,通常只是 ()

标签: c++ inheritance subclass this-pointer


【解决方案1】:

TellWidth() 很可能在 BMP 类中声明为 private(或没有访问器修饰符)。它必须是protectedpublic 才能使Image 类能够访问它,并且它还需要是virtual,如果您希望能够在Image 类中覆盖它。

正确的this用法是这样的:

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

阅读this 以获取有关this 的快速教程。

【讨论】:

  • TellWidth() 是公开的。当你使用你提到的用法时,编译器告诉我类 Image 没有成员名称 TellWidth。
【解决方案2】:

关于this 的一点:你很少需要明确提及它。通常的例外是当您需要将其传递给非成员函数时(这里似乎不是这种情况。)

当您在类成员函数内部时,this-&gt;field 可以简单地作为field 访问,this-&gt;function(x) 可以作为function(x) 调用。

以下是您的代码中的一些 cmets。我希望它们对您有所帮助。

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;
}

编辑:看起来有人将same course 作为 OP。此处提供的示例更清楚地表明 Image 应该具有某种数组访问器,这可以解释 (*this)(x,y)-&gt;Red = (255 - (*this)(x,y)-&gt;Red) 的意图。

编辑 2Here's the source 用于原始 BMP 类。

【讨论】:

    【解决方案3】:

    类Image被定义为

    class Image : public BMP  
    {
    public:
    
        void invertcolors();
    
        void flipleft();
        void adjustbrightness(int r, int g, int b) ;
    
    };
    

    【讨论】:

    • BMP定义为什么?
    • class BMP{ private int Width;整数高度;公共: int TellBitDepth(void) 常量; int TellWidth(void) 常量; int TellHeight(void) 常量; };这是课程的一部分,我之前写的另一个太大了,无法发布
    • 请将代码发布为对您问题的修改,而不是答案或 cmets。
    猜你喜欢
    • 1970-01-01
    • 2012-01-27
    • 2023-03-18
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多