Const Correctness!

Started by
10 comments, last by the_edd 12 years, 2 months ago
(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!
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)
Socket != ISocket

Also, random C cast FTL.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


Socket != ISocket

Also, random C cast FTL.


lol i'm retarded thanks guys
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.
Can you edit posts anymore?
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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?
Also, this is in engine code, so it's supposed to see the internals!
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement