Jump to content
  • Advertisement
Sign in to follow this  
jamesleighe

Const Correctness!

This topic is 2497 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

(C++, MSVC)

This line:

const Vector< ISocket* >* sockets = ((NetworkSystem*)NetworkAPI())->GetSockets();


With 'GetSockets(~)' defined as:


const Vector< Socket* >*
NetworkSystem::GetSockets( )


produces the error:
"cannot convert from 'const Vector<T> *' to 'const Vector<T> *'"

What am I doing wrong here? I just want to get a pointer to a const object.

Thanks as usual guys!

Share this post


Link to post
Share on other sites
Advertisement
Post the rest of the error message or examine it youself ... The problem is probably going to be with the two T's.

i.e isn't there some more to the error message like

with
[
_Ty=foo*
]
and
[
_Ty=const foo*
]

(or whatever)

Share this post


Link to post
Share on other sites
The random cast is to access the internal version of the network system, NetworkAPI() is meant to be used by the game code.

It's definitely ugly tho.
Thanksfully super rarely used.

Share this post


Link to post
Share on other sites
Casting anything that involves an inheritance hierarchy, using C casts, is dangerous. More importantly, casting something so that you can get access to its internals when you hid those internals away suggests you've got a design issue that you should take a strong hard look at.

Share this post


Link to post
Share on other sites
Well, it's not hidden it's just that NetworkAPI() is defined:


FORCEINLINE INetworkSystem* NetworkAPI( )
{
extern INetworkSystem* _g_NetworkSystemInstance;
Assert( _g_NetworkSystemInstance );
return _g_NetworkSystemInstance;
}


And I only expose game code stuff through 'INetworkSystem' so I needed a 'NetworkSystem'.

How could I improve this?

Share this post


Link to post
Share on other sites
When in C++...

If you're going to be casting the pointer, use a static_cast (in this case). As far as how to improve it, I don't know what INetworkSystem looks like, nor what you're using this vector of sockets for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!