类定义实例化实例化问题 [英] Class Definition Instance Instantiation Question

查看:150
本文介绍了类定义实例化实例化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 imgmanager ,允许我加载所有的图片一次,这是相当不错,而原型我有我的所有文件在一个地方,所以我不必担心循环定义。

我的头文件

pre> #ifndef IMAGEMANAGER_H
#define IMAGEMANAGER_H
#includeImg.h
#include< vector>
#include< map>
#include< string>

class imgmanager {
protected:
std :: vector< sf :: Image *>图片;
std :: map< std :: string,int>位置;
public:
sf :: Image * addimg(std :: string path); //相对于资源
sf :: Image * getimg(std :: string path);
int size();
virtual〜imgmanager();
sf :: Image * operator [](int);
} imagemgr;

#endif

我的编译器向我抱怨:



所以我问:我应该怎么做才能拥有我的imagemgr类的全局实例?我应该创建一个全局头文件并创建一个实例吗? (在这种特殊情况下,我可以只在我的main.cpp中创建一个全局变量,没有任何标题需要该实例)



解决方案

不要在标头中创建对象实例。



一个源文件中创建对象实例。



如果您需要跨多个翻译单位访问它,请将其放在您的标题中:

  extern imgmanager imagemgr ; //声明

这将通知所有可以看到命名对象;但它仍然只会在您写的一个源文件中实际定义:

  imgmanager imagemgr; // definition 

(这类似于声明

  void f(); //声明
void f(){...} // definition








上面给出的一般忠告已经完全传达,如果你只是去使用一个,单个,全局的实例。要么使其成为singleton类,要么在命名空间中使用自由函数。


I have a class imgmanager that allows me to load all my images exactly once, it's quite nice, and while prototyping I had all of my files in one place, so I didn't have to worry about cyclical definitions. However after separating all of my classes I have a problem.

My Header File

#ifndef IMAGEMANAGER_H   
#define IMAGEMANAGER_H
#include "Img.h"
#include <vector>
#include <map>
#include <string>

class imgmanager{
 protected:
 std::vector<sf::Image*> images;
 std::map<std::string,int> positions;
 public:
 sf::Image* addimg(std::string path); //relative to resources
 sf::Image* getimg(std::string path);
 int size();
 virtual ~imgmanager();
 sf::Image* operator[](int);
}imagemgr;

#endif

With the instance created after the } and before the ; my compiler complains at me:

So I ask: What should I do to have a global instance of my imagemgr class? Should I just make a global header file and create an instance? (in this particular case I can just make a global variable in my main.cpp, none of the headers require the instance)

解决方案

Don't create object instances in headers.

Create your object instance in one source file.

If you need to access it across multiple Translation Units, put this in your header:

extern imgmanager imagemgr; // declaration

This will inform all code that can "see" the header that there exists a so-named object; but it will still only actually be defined in the one source file where you wrote:

imgmanager imagemgr; // definition

(This is analogous to the way in which you declare functions in a header, but define them in precisely one source file:

void f(); // declaration
void f() { ... } // definition

)


The above general advice dutifully imparted, I would now question the rationale of having a class at all if you're only going to use one, single, global instance of it. Either make it a "singleton" class, or use free functions in a namespace instead.

这篇关于类定义实例化实例化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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