Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Const Correctness!


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
11 replies to this topic

#1 James Leighe   Members   -  Reputation: 217

Like
0Likes
Like

Posted 11 February 2012 - 10:32 PM

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

Ad:

#2 jwezorek   Crossbones+   -  Reputation: 1332

Like
1Likes
Like

Posted 11 February 2012 - 10:44 PM

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)

#3 Washu   Senior Moderators   -  Reputation: 3113

Like
1Likes
Like

Posted 11 February 2012 - 10:44 PM

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.
ScapeCode - Blog | SlimDX


#4 James Leighe   Members   -  Reputation: 217

Like
0Likes
Like

Posted 11 February 2012 - 10:46 PM

Socket != ISocket

Also, random C cast FTL.


lol i'm retarded thanks guys

#5 James Leighe   Members   -  Reputation: 217

Like
0Likes
Like

Posted 11 February 2012 - 10:48 PM

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.

#6 James Leighe   Members   -  Reputation: 217

Like
0Likes
Like

Posted 11 February 2012 - 10:48 PM

Can you edit posts anymore?

#7 Washu   Senior Moderators   -  Reputation: 3113

Like
2Likes
Like

Posted 11 February 2012 - 10:49 PM

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.
ScapeCode - Blog | SlimDX


#8 James Leighe   Members   -  Reputation: 217

Like
0Likes
Like

Posted 11 February 2012 - 10:56 PM

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?

#9 James Leighe   Members   -  Reputation: 217

Like
0Likes
Like

Posted 11 February 2012 - 10:56 PM

Also, this is in engine code, so it's supposed to see the internals!

#10 Washu   Senior Moderators   -  Reputation: 3113

Like
0Likes
Like

Posted 11 February 2012 - 11:01 PM

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.
ScapeCode - Blog | SlimDX


#11 James Leighe   Members   -  Reputation: 217

Like
0Likes
Like

Posted 11 February 2012 - 11:17 PM

Just building a thread to process my async sockets no biggie.

I used to use C++ casts but man... they sure are hilariously verbose.
I also turn off RTTI since I don't use it.

Anyhow, thanks for your help.

#12 e‍dd   Members   -  Reputation: 2042

Like
0Likes
Like

Posted 11 February 2012 - 11:37 PM

I used to use C++ casts but man... they sure are hilariously verbose.

That's a good thing. You want something that's conceptually ugly to be aesthetically ugly too.

Also with C++-style casts, it's much harder to accidentally cast more than you intended, as the C++ compiler will tell you that you've e.g. cast off const-ness unintentionally in a static_cast.




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