Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kern

Member template function

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this function in my class (CSystemManager)
  template <class Type> Type *TGet(WORD wId) {
	return reinterpret_cast<Type *>(Get(wId));
}  
Get is a virtual function that also belongs to this interface. It returns a CSystem *. When I try to use this function like so
  CDataSystem *pDataSys = pManager->TGet<CDataSystem>(2);
  
I get ''CDataSystem'' : illegal use of this type as an expression. I don''t understand what''s wrong with my code, and how to make things right. Any help would be apreciated.

Share this post


Link to post
Share on other sites
Advertisement
Which compiler are you using? Neither VC++6 nor VC++.net 2002 can handle member templates AFAIK. (So you cannot implement the pretty NullClass from Effective C++, Chapter 25 *g).

Furthermore it looks like you are using a very dangerous kind of downcasting. You should either redesign your program a bit or at least use dynamic_cast.

Regards,

Andre


-----------------------------
Andre Loker
GentleStorm Studios - Germany
http://www.gentlestorm.de

[edited by - VizOne on March 30, 2003 12:36:42 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
reinterpret_cast is usually not what you want. If Get() returns a System*, you probably want to static_cast<> to the derived class (dynamic_cast<> if you use virtual inheritance)

Share this post


Link to post
Share on other sites
I''m using VC++6 Enterprise Edition.
That means there is no solution at all? (except changing compiler)

Your right about the cast, I just wonder why VC++ sugested I used reinterpret or C style cast when I used none.

Thanks for the info.

Share this post


Link to post
Share on other sites
I found this on Microsoft''s website in the VC++ FAQ

Does Visual C++ 6.0 support member templates?
Member templates defined inside the class body are supported in Visual C++ 6.0.

So in theory thats not the problem, we are left with what''s wrong with my program?

Share this post


Link to post
Share on other sites
It does work, but (I think) only if a class Type variable is passed.

If you would try :

template Type *TGet(WORD wId,Type) {
return reinterpret_cast(Get(wId));
}


This would work but, however, it's only a hack :-(

BlackHC


I do know that I don't know anything.

[edited by - BlackHC on March 31, 2003 10:54:52 AM]

Share this post


Link to post
Share on other sites
Why not just do this and safe yourself writing another class member function:


  
CDataSystem *pDataSys = dynamic_cast<CDataSystem *>(pManager->Get(2));

Share this post


Link to post
Share on other sites
Your right about it, I tried this

  
template <class Type> Type TGet(WORD wId, Type pNull) {
return dynamic_cast<Type>(Get(wId));
}

CDataSystem *pDataSys = pManager->TGet(2, pDataSys);


It works, but I have to send a useless param...
I guess it will have to do until I remove the need for casting.

Thanks for your help.

Share this post


Link to post
Share on other sites
fizban75
The goal of this template member function is to hide that incredibly long code.

Actualy the original line looks like this

    
m_pList = dynamic_cast<CExecutable *>(dynamic_cast<CDataSystem *>(Engine().Manager()->Get(2))->Add(&SNodeTag(TL_EXEC, 2, 0, 1, "../Resource/Mesh.lst")));


It preferable to hide it don't you think?

[edited by - Kern on March 31, 2003 11:34:10 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Kern
fizban75
The goal of this template member function is to hide that incredibly long code.

Actualy the original line looks like this


      
m_pList = dynamic_cast<CExecutable *>(dynamic_cast<CDataSystem *>(Engine().Manager()->Get(2))->Add(&SNodeTag(TL_EXEC, 2, 0, 1, "../Resource/Mesh.lst")));


It preferable to hide it don''t you think?

[edited by - Kern on March 31, 2003 11:34:10 AM]


hiding is never good. it shows you have something messed up wich you don''t want to show, not?

split the line into several..

  
CDataSystem* dataSystem = dynamic_cast<CDataSystem*>(Engine().Manager()->Get(2));
m_pList = dynamic_cast<CExecutable*>(dataSystem->Add(&SNodeTag(TL_EXEC,2,0,1,"../Resource/Mesh.lst"));

thats two lines, and a bit more readable imho
then again.. how about direct member functions in your engine to get the different managers?
CDataSystem* dataSystem = Engine().DataSystem(); would be much simpler. and don''t talk about general purpose, please:D

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!