如何在另一个类中使用成员变量 [英] how to use member variables in another class

查看:173
本文介绍了如何在另一个类中使用成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的程序有点麻烦。我写了两个班级

Hi all,
I have some trouble with my program.I wrote two classes

class LoadImage
{
 public:
     ......
     int width;
     int height;
     Byte *imgData;
};



在上面的课程中我加载一个图像,并获取有关图像宽度,高度的信息,我想在下面的类中处理图像;


In the class above I load an image,and get the information about width, height of the image,I want to process the image in the following class;

class ProcessImage
{
 ....
};
void ProcessImage::onProcess()
{
  LoadImage img;
  int lwidth=img.width; // this is the wrong way I used!!
  ...
}


在ProcessImage类中的
,我会频繁使用类LoadImage中的成员变量,但在我加载图像后,类LoadImage中的成员变量都被销毁了,我在处理图像时如何使用这些变量?


in the class ProcessImage, I will use the member variables in class LoadImage frequently,but after I load the image, the member variables in class LoadImage are all destroyed, how can I use these variables when I process the image?

推荐答案

首先,不要访问成员其他类的变量直接。这是非常糟糕的做法。您的成员变量应该是私有的,您应该声明方法(函数)来返回值。这可以防止调用者将它们设置为无效值。



第二,当你到达那条线时,为什么会员变量被破坏?他们不应该。更有可能的是你没有初始化它们。



您正在使用默认构造函数来创建LoadImage对象。这是你想要的,或者你想指定文件路径或资源ID,例如?



你有一个正确初始化成员的默认构造函数吗?变量(可能为零)。



这个怎么样,例如:

In the first place, don''t access member variables of other classes directly. This is very bad practice. Your member variables should be private and you should declare methods (functions) to return the value. This prevents a caller from setting them to invalid values.

In the second place, why are the member variables "destroyed" when you reach that line? They shouldn''t be. What is more likely is that you haven''t initialised them.

You are using the default constructor to create the LoadImage object. Is this what you want, or do you want to specify a file path or a resource id, for example?

Do you have a default constructor that correctly initialises the member variables (probably to zero).

How about this, for example:
class LoadImage
{
public:
    LoadImage()
    :   width(0),
        height(0),
        imgData(NULL)
    {
    }

    LoadImage
        (
        const std::string& filePath
        )
    :   width(0),
        height(0),
        imgData(NULL)
    {
        // load image from filePath into imgData and set height and width accordingly.
    }

    virtual ~LoadImage()
    {
        if (NULL != imgData)
        {
            // Free imgData according to how you initialised it
            // And set it back to null (not important in this case, but it is good practice)
            imgData = NULL;
        }
    }

    inline int GetHeight() const
    {
        return height;
    }

    inline int GetWidth() const
    {
        return width;
    }

    // ......

private:
    int width;
    int height;
    BYTE* imgData;
};


您声明LoadImage但从未创建关联对象。您需要创建对象以便可以访问成员数据,例如:



LoadImage img = new LoadImage();

int lwidth = img.Width;



否则你试图访问受保护的内存,或者它甚至可能是一些随机值,因为它没有初始化。
You declare LoadImage but you never create the associated object. You need to create the object so that you can access the member data, like:

LoadImage img = new LoadImage();
int lwidth = img.Width;

Otherwise you are attempting to access protected memory or it could even be some random value because its not initialized.


这篇关于如何在另一个类中使用成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆