• Popular Now

• 13
• 18
• 19
• 27
• 10

This topic is 2033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Maybe you're familiar with this but I'm not.Untill now I never saw a friend function.

Here's the code:

[source lang="cpp"]#ifndef TEXTUREMGR_H
#define TEXTUREMGR_H

#include "d3dUtil.h"
#include <vector>
#include <string>

class TextureMgr
{
public:
friend TextureMgr& GetTextureMgr();
typedef std::vector<std::wstring> StringVector;

void init(ID3D10Device* device);

// for debug
void dumpInfo()const;

std::wstring arrayName,
const std::vector<std::wstring>& filenames);

// .dds files can store cube textures in one file

private:
TextureMgr();
TextureMgr(const TextureMgr& rhs);
TextureMgr& operator=(const TextureMgr& rhs);
~TextureMgr();

void buildRandomTex();

private:
ID3D10Device* md3dDevice;

StringVector mTextureNames;

};

TextureMgr& GetTextureMgr();

#endif // TEXTUREMGR_H[/source]

[source lang="cpp"]TextureMgr& GetTextureMgr()
{
static TextureMgr tm;

return tm;
}[/source]

In my book it says I can only have 1 texture manager.So a friend function gets acces to everything in a class,and that GetTextureMgr() also returns a TextureMgr type of object.

But why do we need an adress? friend TextureMgr& GetTextureMgr();

Can someone give me a good explanation?

Share on other sites
This is not an address (a.k.a. pointer), but rather a reference. But you would use both for the same reason: If you didn't use a reference (or pointer), you would actually return a copy of your TextureMgr (which is not what you want). By using a reference (the ampersand behind your type), you tell the compiler not to create a copy of your TextureMgr, but instead return the very same object to the caller of GetTextureMgr. This has nothing to do with the friend declaration. In the friend declaration, you just repeat the signature of the function you want to be a friend of your class. So, because your function has the signature "TextureMgr& GetTextureMgr()", you have to use "friend TextureMgr& GetTextureMgr()" in your class.

Now that we have the on-topic discussion done, here is some more advice: Even though your book tells you to use a singleton class (one for which there can only be one instance), this is in most cases (not in all, but definitely in this case) not the best (nor the easiest) solution to the problem. In fact, there is no reason not to allow a second texture manager, be it for another render target or so you can switch them out for different situations in your game. Also, this implementation of a singleton is a bit away from what most would consider a "default" implementation. But that's the least of its problems.

What book is this? It does not seem to teach a very clean programming style.

Share on other sites
Introduction to 3d game programming with directx 10

Also wouldn't it be enough just to decleare a static object of type texturemgr and use it everywhere I need it? It's pretty hard to understand what's going on here...

Sorry but i'm still very confused... Edited by noatom

Share on other sites
The friend declaration says that only that function is a friend. The functions declaration is:
 TextureMgr& GetTextureMgr(); 

Friending the function gives the function an access to its private constructors, since it can't be instantiated otherwise, and returns a reference to static TextureMgr object. Edited by Codarki

Share on other sites
If you just declare a variable somewhere you have absolutely no control over the initialization sequence. On the other hand, currently you know the singleton is initialized on the first call to GetTextureMgr, which is better.

Of course a singleton is still a singleton and a bad code smell is still a bad code smell, as rnlf already said.

Just to add, if something as fundamental a references gives you pause you should really reconsider what you are doing. Learning C++ and learning 3D graphics at the same time is simply a recipe for disaster.