C++ 'this'指针,使用'this'指针继承子类中超类的函数
嗨,我正在努力理解如何使用“this”指针。现在我编写了一个示例程序,它使用一个类Image,它是BMP类的一个子类。现在,函数TellWidth和TellHeight在BMP类中声明。现在编译器给了我一个错误,它说TellWidth函数在映像中不存在。但是,由于图像是BMP的一个子类,它不应该继承BMP中的功能。 我如何解决这个问题C++ 'this'指针,使用'this'指针继承子类中超类的函数,c++,inheritance,subclass,this-pointer,C++,Inheritance,Subclass,This Pointer,嗨,我正在努力理解如何使用“this”指针。现在我编写了一个示例程序,它使用一个类Image,它是BMP类的一个子类。现在,函数TellWidth和TellHeight在BMP类中声明。现在编译器给了我一个错误,它说TellWidth函数在映像中不存在。但是,由于图像是BMP的一个子类,它不应该继承BMP中的功能。 我如何解决这个问题 void Image :: invertcolors() { int x; int y; int width =(*this).Tel
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 BMP {
private:
int Width;
int Height;
public:
int TellBitDepth(void) const;
int TellWidth(void) const;
int TellHeight(void) const;
};
TellWidth很可能声明为私有,或者在BMP类中没有访问器修饰符。它需要被保护或公开,以便Image类能够访问它,如果您希望能够在Image类中重写它,它还需要是虚拟的
正确的用法如下:
int width = this->TellWidth();
int height = this->TellHeight();
阅读有关此的快速教程。TellWidth很可能声明为私有,或者在BMP类中没有访问器修饰符。它需要被保护或公开,以便Image类能够访问它,如果您希望能够在Image类中重写它,它还需要是虚拟的
正确的用法如下:
int width = this->TellWidth();
int height = this->TellHeight();
请阅读有关此的快速教程。类图像定义为
class Image : public BMP
{
public:
void invertcolors();
void flipleft();
void adjustbrightness(int r, int g, int b) ;
};
类映像定义为
class Image : public BMP
{
public:
void invertcolors();
void flipleft();
void adjustbrightness(int r, int g, int b) ;
};
关于这一点,有一点:你很少需要明确地提到它。通常的例外情况是,当您需要将其传递到非成员函数中时,这里的情况似乎不是这样
当您在类成员函数中时,此->字段可以简单地作为字段访问,而此->functionx可以作为functionx调用
下面是对您的代码的一些注释。我希望他们能帮上忙
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。这里给出的示例更清楚地表明,图像应该具有某种数组访问器,这可能解释了*thisx,y->Red=255-*thisx,y->Red的目的
编辑2:对于原始BMP类。关于这一点,您很少需要明确提及它。通常的例外情况是,当您需要将其传递到非成员函数中时,这里的情况似乎不是这样
当您在类成员函数中时,此->字段可以简单地作为字段访问,而此->functionx可以作为functionx调用
下面是对您的代码的一些注释。我希望他们能帮上忙
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。这里给出的示例更清楚地表明,图像应该具有某种数组访问器,这可能解释了*thisx,y->Red=255-*thisx,y->Red的目的
编辑2:用于原始BMP类。TellWidth是公共的。当你使用你提到的用法时,编译器告诉我类Image没有成员名TellWidth.TellWidth是public。当你使用你提到的用法时,编译器告诉我类Image没有成员名TellWidth。类BMP定义为什么?类BMP{private int-Width;int-Height;public:int-TellBitDepthvoid const;int-TellWidthvoid const;int-TellHeightvoid const;};这是我之前编写的另一个类的一部分,太大了,不能将代码作为问题的编辑而不是答案或注释发布。BMP类定义为什么?BMP类{private int-Width;int-Height;public:int-TellBitDepthvoid const;int-TellWidthvoid const;int-TellHeightvoid const;};这是我之前编写的另一个类的一部分太大,无法将代码作为问题的编辑发布,而不是作为答案或注释发布。请发布头文件中声明类Image和BMP的部分。包括类声明本身,以及反转颜色、TellWidth和TellHeight的声明。还包括用于表示位图数据的任何类型的数据声明,例如数组和/或名为x和y的字段声明(如果碰巧存在),请参见下面的回答。您可能可以删除头文件中的所有其他内容,至少现在是这样。还请包括任何名为Red、Green或Blue的字段或方法的定义。如果我删除不相关的Red/Green/Blue部分并用*this->修复明显的错误,这对我来说很好。也许你有链接的问题,而不是编译的问题?也许这些函数没有定义,或者它们的单元没有包含在链接过程中?同样,这也不太可能,但也许您正在使用一些奇怪的编译器,不喜欢这些函数的无效形式参数列表?它是完全有效的,但很少在C++中使用,通常只是。请张贴声明类图像和BMP的头文件的部分。包括类声明本身,以及反转颜色、TellWidth和TellHeight的声明。还包括用于表示位图数据的任何类型的数据声明,例如数组和/或
如果名为x和y的字段声明碰巧存在,请参见下面的答案。您可能可以删除头文件中的所有其他内容,至少现在是这样。还请包括任何名为Red、Green或Blue的字段或方法的定义。如果我删除不相关的Red/Green/Blue部分并用*this->修复明显的错误,这对我来说很好。也许你有链接的问题,而不是编译的问题?也许这些函数没有定义,或者它们的单元没有包含在链接过程中?同样,这也不太可能,但也许您正在使用一些奇怪的编译器,不喜欢这些函数的无效形式参数列表?它是完全有效的,但很少在C++中使用,通常它只是。