【发布时间】:2015-06-11 16:41:00
【问题描述】:
我创建了一个基于 FLTK 的 Fl_Input-class 的定制对象,该对象绘制屏幕输入框并自动将其放置在 previous 框的下方宣布。这是通过使用“外部”(在“类实现外部”的意义上)计数器来实现的,该计数器用于计算框的 y 坐标。
这是该类的简化版本:
// Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
class Example : public Fl_Input {
public:
Example(const char * str); // constructor
private:
int ex_y(); // compute y-coordinate of current box based on the previous ones
};
#endif // EXAMPLE_H
这里是成员函数和构造函数:
// Example.cpp
#include "Example.h"
int inbox_y1; // y-coord of first box; an example of “external” constants
int inbox_num; // counter: number of input boxes declared; used to compute their y-coord
Example::Example(const char * str)
: Fl_Input(200, ex_y(), 200, 25, str)
{
++inbox_num;
}
int Example::ex_y()
{
return inbox_y1 + (25+25)*inbox_num;;
}
最后是主文件(我将对象的声明包装在自定义函数中):
// main.cpp
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Window.H>
#include <string>
#include <iostream>
#include "Example.h"
void place_inboxes()
{
int inbox_y1 = 25; // not used by the following objects: the first one is attached to the upper window
int inbox_num = 0; // the counter is not incremented by the following declarations
Example * ex1 = new Example("First Input Box");
std::cout << "First call: " << inbox_num << '\n';
Example * ex2 = new Example("Second Input Box");
std::cout << "Second call: " << inbox_num << '\n';
Example * ex3 = new Example("Third Input Box");
std::cout << "Third call: " << inbox_num << '\n';
}
int main()
{
Fl_Window * win = new Fl_Window(600, 600, "Test of 'Example' class");
{
place_inboxes();
}
win->end();
win->show();
return Fl::run();
}
这编译和链接成功,但是(即使盒子确实是堆叠的)结果不是我所期望的:
- 第一个框连接到窗口的上部,而它应该位于窗口下方,距离由(“外部”)常量
inbox_y1定义。这让我觉得Example-type 对象的声明没有考虑到这样的常量。 - 在每个新对象声明之后,都会打印计数器的递增值,但实际上它不受影响。
问题: 我怎样才能让它正常工作? (我的印象是我在各种源文件中声明常量时弄得一团糟......)
(可选)附带问题: 是否定义了一个既可以构建对象又可以将其视为不好的做法的类?我的意思是,让一个对象一次做“太多”的事情是不好的做法吗? (我也想写一个函数来放置对象,但我什至不知道从哪里开始。)
最后说明: 我现在使用的代码实际上运行良好,但它有一个源文件(我称之为“外部”的常量是全局常量)。拆分成多个源文件时出现问题。
【问题讨论】:
-
Example.cpp 中的 inbox_y1 和 inbox_num 是全局变量,而 place_inboxes() 中的 inbox_y1 和 inbox_num 是局部变量,尽管它们有相同的名字。我认为无论如何在 Example 之外进行位置计算会更干净,也许在另一个类中表示类似一堆输入框的东西。
-
inbox_y1 和 inbox_num 未初始化。它们可能为零或随机浮动到一个值。尝试初始化它们,看看你是否得到了想要的效果。
-
@cup:感谢您的建议。 ATM,我通过在 Example.cpp 中将
inbox_y1和inbox_num声明为extern并在 main.cpp 中将它们定义为 global 变量来使其工作。但是,我将这些最后的定义 insideplace_inboxes()只是为了避免全局变量。有没有更好的办法?