Jump to content

  • Log In with Google      Sign In   
  • Create Account

Friend and adress function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 24 August 2012 - 01:31 AM

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; ID3D10ShaderResourceView* getRandomTex(); ID3D10ShaderResourceView* createTex(std::wstring filename); ID3D10ShaderResourceView* createTexArray( std::wstring arrayName, const std::vector<std::wstring>& filenames); // .dds files can store cube textures in one file ID3D10ShaderResourceView* createCubeTex(std::wstring filename);private: TextureMgr(); TextureMgr(const TextureMgr& rhs); TextureMgr& operator=(const TextureMgr& rhs); ~TextureMgr(); void buildRandomTex();private: ID3D10Device* md3dDevice; StringVector mTextureNames; std::vector<ID3D10ShaderResourceView*> mTextureRVs; ID3D10ShaderResourceView* mRandomTexRV;};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?

Sponsor:

#2 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 24 August 2012 - 01:59 AM

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.

my blog (German)


#3 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 24 August 2012 - 05:12 AM

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, 24 August 2012 - 05:30 AM.


#4 Codarki   Members   -  Reputation: 462

Like
1Likes
Like

Posted 24 August 2012 - 08:08 AM

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, 24 August 2012 - 08:08 AM.


#5 BitMaster   Crossbones+   -  Reputation: 4432

Like
0Likes
Like

Posted 24 August 2012 - 08:11 AM

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.

#6 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 24 August 2012 - 10:44 AM

you don't get me...i don't have a problem with references,I had a problem with using them when I decleare a function,I HAD....




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS