1

こんにちは、「this」ポインターの使用方法を理解しようとしています。ここで、クラス BMP のサブクラスであるクラス Image を使用するサンプル プログラムを作成しました。これで、関数 TellWidth と TellHeight が BMP クラスで宣言されました。コンパイラは、TellWidth 関数が Image に存在しないというエラーを表示します。しかし、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;
};
4

3 に答える 3

3

TellWidth()privateとして宣言されている(またはアクセサー修飾子がない)可能性が最も高いBMPです。クラスでアクセスできるようにするには、protectedまたはpublicである必要があり、クラスでオーバーライドできるようにする場合は、である必要もあります。ImagevirtualImage

そして、適切なthis使用法は次のようになります。

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

thisの簡単なチュートリアルをお読みくださいthis

于 2011-01-28T02:21:45.407 に答える
1

についての 1 つのポイント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 クラスのソースは次のとおりです。

于 2011-01-28T03:10:15.943 に答える
0

クラス画像は次のように定義されます

class Image : public BMP  
{
public:

    void invertcolors();

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

};
于 2011-01-28T02:38:07.483 に答える