English 中文(简体)
类定义实例实例化问题
原标题:Class Definition Instance Instantiation Question
  • 时间:2011-06-01 14:29:26
  •  标签:
  • c++

我有一个类imgmanager,它允许我只加载一次所有的图像,这很好,而且在原型设计时,我把所有的文件都放在一个地方,所以我不必担心循环定义。然而,在我把所有的课都分开之后,我遇到了一个问题。

我的头文件

#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

在}之后和之前创建的实例;我的编译器向我抱怨:

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

最佳回答

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

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

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

extern imgmanager imagemgr; // declaration

这将通知所有可以“看到”标头的代码存在这样命名的对象;但它实际上仍然只能在您编写的一个源文件中定义:

imgmanager imagemgr; // definition

(这类似于在标头中声明函数,但仅在一个源文件中定义的方式:

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

)


上面的一般性建议尽职尽责地给出了,如果你只打算使用一个单一的全局实例,我现在会质疑拥有一个类的理由。要么让它成为一个“singleton”类,要么在命名空间中使用免费函数。

问题回答

If you require a single global instance, i suggest you make the ImageManager a "Singleton". I m unsure what to do for complex types, but for a global declaration of a simple data-type you should declare the variable "extern" in the header, and instantiate it in exactly one module (.cpp file).





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签