"Smart" Constructor

Started by
15 comments, last by AdmiralBinary 22 years ago
Go and look up the virtual constructor idiom. What you''re trying to do is well known, you''ve just done it wrong, that''s all.
Advertisement
Thanx guyz
quote:Just commenting on the first snippet of code posted :

this = new CChatMessage(netConnection);


The fact that you are assigning to the this pointer will not affect the object you have constructed (except for whatever member functions that you might call later in the constructor, to which this is transparently passed).

If this ever compiles, all you''ll get is a memory leak.

Yeah, my second post actually states that I had to rewrite the code to get it to compile. I didn''t say how, though - this is what I did:

  CMessage * msg = this;delete msg;msg = new CChatMessage;  

And it worked...
quote:What your are describing is similar to a "factory" pattern which is a perfectly acceptable OOP method.

OK, thanx - I thought of using a CNetConnection::GetMessage() and using it to return the right type of message, but at the time I thought it''d be "prettier" to do it the other way Oh well, we learn something new every day... Thanx, BTW - I''ll look into the factory thingy.
quote: first off, did you get that to compile? i thought that modifying this was illegal.

Yeah, I had to change it to get it to compile
quote: Hmmm... why arent you working on stick soldiers 2... someone deserves a beating...

Erm...well...yes - it''s coming along nicely (slowly), and we''re making great progress (we got the first few lines of code working) - we expect to have it finished real soon (look out for it in a coupla years).

Thanx again for your help everyone.

---------------

#define TRUE 0
#define FALSE 1
//MUAHAHAHAHAAAA!
Yep, you want a factory.

I have a CMessageFactory class that has three methods, IMessage* IMessage* CreateMessage(u8* pStream, u32 bytes)
bool RegisterMessage(IMessage*)
bool UnregisterMessage(IMessage*)
(and you probably don''t really need the UnregisterMessage one)

I create an empty message and use with the register call of the factory. IMessage is a reference counted interface, and has a Clone method. The factory maintains a map from the 32bit msgID to the registered IMessage*. When CreateMessage is called the factory sucks the msgID out of the stream, looks it up in the map, and uses found IMessage*''s Clone method to make a new message of the correct type. The new message then is responsible for deserializing itself from the reminder of the stream, where-upon it''s finally returned from the CreateMessage function.

It''s not perfect, but it works out reasonably well.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
AB you better be joking man...
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Even better, plugable factories.

I think the article is at the site
"How Plugable Factories rock my multiplayer world" or something to that effect.

Has anyone else tried this? It seems to work pretty good.
I read that article when I started working on my message system - it''s all good ideas, but it''s incomplete. IIRC, it doesn''t detail how you register messages at run-time, nor how the serialization/deserialization works.

You almost want a IBlob structure that reference-count manages network buffers - kinda how IMediaSample works in DirectShow. But you also want a (message) structure that makes sense of what''s in that blob.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
At load time, the static member causes them all to register. If I remember correctly, the sample code with the article is indeed incomplete, you just need to declare the statics to get it to compile.

As far as serialization goes, I wrote a to/fromWire function that spits out/read in a string and the length. It is all really basic code, so I wrote a quick PERL script that writes it all for me. Just a bunch of memcopies.

This topic is closed to new replies.

Advertisement