C++ 中的静态构造函数在我自己的操作系统内核上
我正在尝试用 C++ 编写内核,并且我是操作系统开发的初学者。现在我正在实现 cout 以在监视器上显示,但我遇到了一些问题。我知道我问的问题很愚蠢,但我也是 C++ 的新手。
我编写了一个 OStream 类,它调用系统函数 write
以在屏幕上显示。它的基类是我的视频类。该类包含在命名空间 std 中。所以主要问题是,当我创建 OStream 对象时,它没有调用它的构造函数,因此也没有调用其基类的构造函数,因此视频内存没有初始化,因此屏幕上不会显示任何内容。
这是我的代码:
/*OStream.h*/
namespace std{
class OStream:public Video{
public:
OStream();
OStream& operator<<(int);
OStream& operator<<(String);
OStream& operator<<(char *cp);
OStream& operator<<(char c);
OStream& operator<<(unsigned char *cq);
OStream& operator<<(unsigned char c1);
};
extern OStream cout;
}
/*OStream.cpp*/
namespace std{
OStream cout;
OStream::OStream(){}
OStream& OStream::operator<<(char *cp){
write(cp);
}
.
.
.
.
}
在上面的代码中,我在 OStream.cpp 文件本身中创建了一个 OStream
类的对象。 但是,如果我在主模块中创建一个对象,那么它会成功调用其构造函数,但随后 cout
不起作用。
这意味着如果我显式创建一个对象,它会完美地工作,但是如何隐式创建一个对象。
我的 Interrupt.cpp 模块也发生了同样的事情。
所以请帮我解决此类问题。 请帮我。 任何帮助将不胜感激。 谢谢。
I am trying to write a kernel in c++ and I am a beginner in OS development. Now I am implementing cout
to display on a monitor, but I am facing some problems. I know the question I am asking is foolish, but I am a newbie in c++ as well.
I have written an OStream class which calls the system function write
to display on the screen. Its base class is my Video class. This class is included into namespace std. So the main problem is that when I create a object of OStream, it is not calling it's constructor and hence not calling the constructor of its base class, so that videomemory is not initialising and therefore nothing will be displayed on screen.
Here is my code:
/*OStream.h*/
namespace std{
class OStream:public Video{
public:
OStream();
OStream& operator<<(int);
OStream& operator<<(String);
OStream& operator<<(char *cp);
OStream& operator<<(char c);
OStream& operator<<(unsigned char *cq);
OStream& operator<<(unsigned char c1);
};
extern OStream cout;
}
/*OStream.cpp*/
namespace std{
OStream cout;
OStream::OStream(){}
OStream& OStream::operator<<(char *cp){
write(cp);
}
.
.
.
.
}
In the above code I am creating an object of OStream
class in the OStream.cpp file itself.
But if I create an object in my main module then it calls its constructor successfully, but then cout
is not working.
That means if I create an object explicitly it works perfectly, but how can I create an object implicitly.
And same thing is happening with my Interrupt.cpp module.
So please help me to solve this type of problem.
Please help me.
Any help will be appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的
cout
实现不合格。如果您要编写 C++ 标准库,则必须将其编写为符合所述标准(而不是您认为的标准)。其次,您需要实现静态构造函数支持。您还没有指定您的编译器,所以我只能说找出您的编译器将其静态构造函数初始化代码放在哪里,并确保在程序启动时调用它。
与从头开始编写一个库相比,将现有 C++ 库(GCC 的 libstdc++ 或 clang 的 libc++)适应新操作系统可能会更幸运。编写 C++ stdlib 不适合初学者;您很快就会陷入模板元编程的困境。
First, your implementation of
cout
is non-conforming. If you're going to be writing a C++ standard library, you must write it to be conforming to said standard (not to what you think the standard is).Second, you need to implement static constructor support. You haven't specified your compiler, so all I can say is figure out where your compiler puts its static constructor initialization code, and make sure to invoke it on program startup.
You may have more luck adapting existing C++ libraries (GCC's libstdc++ or clang's libc++) to your new OS than writing one from scratch. Writing a C++ stdlib is NOT something for a beginner; you'll be neck-deep in template metaprogramming in no time.
如果不调用对象的构造函数,就不可能创建对象。但是对于像 cout 这样的全局变量,有时构造函数被调用的时间是一个问题。如果不同的 .cpp 文件中有两个全局变量,那么您无法预测将首先构造哪个全局变量。也许这就是您所看到的问题?如果是这样,那么简单的答案是将所有全局变量放入一个 .cpp 文件中(如果您愿意,可以将其称为 globals.cpp)。然后,全局变量将按照您在 .cpp 文件中声明它们的顺序构建(并以相反的顺序销毁)。
It's not possible to create an object without calling it's constructor. But with global variables like
cout
sometimes the time that the constructor gets called is a problem. If you have two global variables in different .cpp files then you cannot predict which global variable will get constructed first. Maybe that is the problem you are seeing? If so then the easy answer is to put all your global variables into one .cpp file (call it globals.cpp if you like). Then the global variables will get constructed in the order that you declare them within that .cpp file (and destructed in the reverse order).