Skip to main content
gamedev.net
🔒 Locked

with-statement in C++ ?

Started by TroneX 15 replies 1.1k views
Original Post
TroneX
TroneX
Long time, no see ;-) Hope you all had a great weekend! So here''s what I am dangling with at the moment: Suppose, you''ve got some kind of structure with some variables. Suppose further, that the structures'' name is pretty long and that you have to go through every variable for some initialization purpose. This might look something like this: m_wcWeaponControl.wsWeaponSystem[m_iWeapon].fDelayBetweenShoots = 0.2f; m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3Speed.x = 40.f; m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.x = 2.5f; m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.y = 0.475f; m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.z = 0.0f; ... This is ugly. So isn''t there something like a with statement in C++ (Pascal had that, imho) with( m_wcWeaponControl.wsWeaponSystem[m_iWeapon] ) { vct3Speed.x = 40.0f; vct3PositionAdd.x = 2.5f; vct3PositionAdd.y = 0.475f; vct3PositionAdd.z = 0.0f; ... } (Yeah, I know that it could have been vct3PositionAdd = D3DXVECTOR3( x, y, z ) as well, .. this was just for the sake of this sample ;-) So what do you say, ... is this possible somehow? Greetings, TroneX
LightBrain website relaunched, BomberFUN only USD 9.99
visit http://www.lightbrain.de
morebeer
morebeer
why not use a pointer?


    
Weapon* p = &m_wcWeaponControl.wsWeaponSystem[m_iWeapon];
p->fDelayBetweenShoots = 0.2f;
p->vct3Speed.x;
etc.
etc.



[edited by - morebeer on May 11, 2003 2:44:04 PM]
eldee
eldee
ah.. silly VB'er
unfortunately, there is no "with" statement in standard C++... i believe that borland had an implementation of a with statement, but that is compiler specific and obviously just does a find-replace.

with statements would be incredibly useful though.. although you run into some scoping problems.

say, you have a struct called employee, and a member variable called "hours".
using this information, consider the following:


int main()
{
employee_s BobSmith;
float hours; // used for something else, say...
// tracking the current time or whatever..
// hypothetical situation regardless.

with(BobSmith)
{
hours = 10; // beep beep beep. we have a scope problem.
}

return 0;
}


an obvious way to fix that would be to make it so you cant do anything *else* inside a "with" block, either that or come up with an entirely new scope resolution operator (or use of it rather, which i've stated on previous threads).
knowing that, consider the same example slightly revised:

int main()
{
employee_s BobSmith;
float hours; // used for something else, say...
// tracking the current time or whatever..
// hypothetical situation regardless.

with(BobSmith)
{
::hours = 10; // the scope resolution operator by
// itself would differentiate itself
// from the "hours" we created inside
// the scope of main()
hours = 24; // using a 24 hour clock! and no scoping problems.
}

return 0;
}


-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!


[EDIT by Oluseyi: Comments widening page.]

[edited by - Oluseyi on May 13, 2003 10:43:56 AM]

TroneX
TroneX
VB sux ;-) I remembered that with statement from my pascal time which is about 10 years ago ;-)

But the pointer idea is worth a further investigation.
I will simply try that out and then I''ll see whether our lead programmer kickz my ass or not ;-)

Thanks!


LightBrain website relaunched, BomberFUN only USD 9.99
visit http://www.lightbrain.de
Prosper/LOADED
Prosper/LOADED
Although the pointer method works, an alias would be more "C++-ish" :

Weapon & p = m_wcWeaponControl.wsWeaponSystem[m_iWeapon];
p.fDelayBetweenShoots = stuff;
Qw3r7yU10p!
Qw3r7yU10p!
quote:
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].fDelayBetweenShoots = 0.2f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3Speed.x = 40.f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.x = 2.5f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.y = 0.475f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.z = 0.0f;
...

This is ugly.



I don't think you understand exactly why it is ugly.

You have code which relies upon knowing about everything right down to the internal of your weapons control system and what the variables are named for velocities and positions. All the guts of your program are hanging out.

C++ supports encapsulation which allows you to hide data and pass it around in a safe and consistent manner.

If you wanted to set the values as above you'd assign a weapon object which would call the object's assignment operator. The operator would do all the fiddly bits and you wouldn't need a 'with' statement because everything would be local to the scope of the object. Nice, clean, obvious.

Read up on copy constructors and assignment operators for classes/structs for a start.

edit: changed 'copy constructor' to 'assignment operator' - hey, it's early! I'm still waking up.

[edited by - petewood on May 12, 2003 4:24:43 AM]
flangazor
flangazor
Don''t use a pointer. Use a reference.
davepermen
davepermen
quote:
Original post by flangazor
Don''t use a pointer. Use a reference.


don''t use anything. use a constructor.

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
daerid
daerid
it would be cool if you did something like this:


  
struct foo
{
int bar;
int* pbar;
};

void main() {
foo f;
with( f ) {
.bar = 7;
->pbar = 10;
}
}


That would handle scoping issues.
daerid@gmail.com
afterburn
afterburn
Scope is not an issue the preprocessor would check this and the scope for an variable that is declared outside of the with it would assume that either its local to the function or the class. Dueing some checking this would not be hard to create in scope checking. just as it does when it compiles
void main(){
int y;
int x;
for(x=0;i<10;x++)
{
y++;
}

}

scope would not matter if its used in the same way that this is.
--What are you nutz?I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.
daerid
daerid
yes, but what happens if you want to use a variable declared outside the with block that has the same identifier as a member variable?

Yes, I know you could just rename the variable, but the point is it''s an unnecessary arbitrary restriction. Requiring the explicit use of the member access operator (.) or the pointer to member operator (->) would remove this restriction.
daerid@gmail.com
Oluseyi
Oluseyi
quote:
Original post by daerid
yes, but what happens if you want to use a variable declared outside the with block that has the same identifier as a member variable?
You use the scope resolution operator, ::. Are you people asleep or do you not know the language?

C++ doesn''t need a with statement. with is just a tool for assuaging guilt over a poor design in most instances.
daerid
daerid
quote:
Original post by Oluseyi
You use the scope resolution operator, ::. Are you people asleep or do you not know the language?



I know the language. However, using the scope resolution operator wouldn''t work if the variable outside the with() block was declared at function scope. The method that I proposed is straightforward, and would be fairly simple to implement in the language parser.

quote:

C++ doesn''t need a with statement. with is just a tool for assuaging guilt over a poor design in most instances.


I agree. It''s those few cases that aren''t most cases where it would be handy.

Don''t get me wrong, I''m not necessarily a fanatic about adding a with() block to the language. I was just offering a suggestion on how to add it to the language without introducing un-needed complexities.
daerid@gmail.com
SabreMan
SabreMan
quote:
Original post by TroneX
This might look something like this:

m_wcWeaponControl.wsWeaponSystem[m_iWeapon].fDelayBetweenShoots = 0.2f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3Speed.x = 40.f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.x = 2.5f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.y = 0.475f;
m_wcWeaponControl.wsWeaponSystem[m_iWeapon].vct3PositionAdd.z = 0.0f;
...

This is ugly.

It''s also bad form to replicate class compositions within referent statements. It means that each time the composition changes, each referent must change in line, which helps your software to resist change.
quote:

So isn''t there something like a with statement in C++ (Pascal had that, imho)

No. The with'' construct is idiotic. It causes some remote lexical scope to be combined with the current scope in a way that changes to one piece of code can affect a lexically unrelated scope. If it''s language idiocy you''re after, then C++ introduces the idea of class scope'', where a class - the building block for object systems - also denotes a scope. Similar to Pascal''s with'', class scope allows you to change the meaning of some piece of code from some lexically distinct other piece of code. Google Law of Demeter'' to find out how to solve your problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.