简体   繁体   English

我可以在静态对象中读取文件(.txt)吗? C++

[英]Can I read a file(.txt) in static object? C++

I have an static object.我有一个静态对象。 File reading code is placed in that object's constructor.文件读取代码放置在该对象的构造函数中。 Is this right or correct code?这是正确的还是正确的代码? Do this code have any problems?这段代码有问题吗?

A.hpp hpp

Class A
{
public:
    A();

private:
    static A someA;
}

A.cpp A.cpp

A A:someA;

A:A()
{
    ofstream myfile;
    myfile.open ("example.txt");
}

Allmost using static is a bad thing.几乎使用静态是一件坏事。 If you must have something that is permanent in your apps you should use the "singleton" design pattern.如果您的应用程序中必须有一些永久性的东西,您应该使用“单例”设计模式。

Having a reference of some class in himself should be made with a pointer and is use for example in a chained list.应该使用指针来引用自己的某个类,例如在链表中使用。

For standard use of class you do this after the description of your class对于类的标准使用,您在类的描述之后执行此操作

int  main( int argc, char**argv )
     {
       A Myclass           (   ) ;
       MyClass.DoSomething (   ) ;
       return              ( 0 ) ;
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM