"with" in vb in C++ ?

Started by
10 comments, last by Daivuk 21 years, 1 month ago
Hi there, I have a class in my c++ game : ex: class Temp{ int S,D,F,G; } so I declare a variable of type Temp: Temp Var; I can access his content like this : Var.S = 1; Var.D = 3; Var.F = 3; Var.G = 5; But in vb, I can write it for faster programing : with Var S = 1 D = 3 F = 3 G = 5 end with My question is, there is a way to make a With like that in c++???
Advertisement
No, there''s no ''with'' statement or anything equivalent in C or C++. Shame really. The best you can do in most cases is to use a reference with a shorter name.

Temp& x = var;
x.S = 1
x.D = 3
x.F = 3
x.G = 5

This would generally be considered a bad idea unless the shorter name was somehow more readable than the longer name.


[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
ha ok.. thanks
I''ll keep the long name so...


Actually your example doesn''t work as the members are private and can''t be accessed in the way you say. This is a Good Thing as it helps arrange and encapsulate your data.

If you are refering to them in a member function you will be able to do so without having to specifiy the object.

If you''re being a good object oriented programmer you wouldn''t want to be doing what you''re suggesting.

I guess you may want to call lots of member functions so ''With'' would be more useful for that. However, if you''re just calling the same sequence of functions on all your objects it may be better to have one function on the object which calls them all. Obviously circumstance will help you decide, but there are other ways to think about your code than wishing it was VB.
There was a pretty gigantic thread on flipcode regarding this. The conclusion was that "with" is harmful because its difficult to read the context of what is going on.

I wouldn''t be too worried about how fast you can type something. If its at all non-trivial, it shouldn''t be an issue. If it is trivial, feel free to use VB then!
I know about Oriented Object thing...

But there is for a description of all units in my game.
(game like starcraft)

So I have a array [52] (52 units in the game)
And constants..
ex : const int Marine = 0;

MyNewUnit = UnitDesc[Marine];

So, I have to set all the shit in UnitDesc[Marine]...
UnitDesc[Marine].Life = 40;
UnitDesc[Marine].Shield = 0;

So set and get will be a waste of time here.
I have 52 Units to do like that and dont want to store it in a .dat file.

oh, and sorry about my english, I''m from quebec.

oh, and I mean BINARY file, not .dat file.
You know what I mean.

Are all the units created with the same default values? You can use the constructor to create them with the correct values.

Get and Set is not the point.

If you want something to move to a new position you might say

Movement newPosition(x, y, z);
unit.Move(newPosition);

This is instead of saying

unit.SetX(x);
unit.SetY(y);
unit.SetZ(z);

You initialise the object with meaningful values and then have it do meaningful things (which will indirectly change the internal values).
Ya I have a Move() function in ma class
But not its not all the same value !
Depending wich unit it is.
UNit_DESC[52] is a template of all unit.
So it''s ok now! I just wanted to know if there is an equivalent for WITH in c++.
Now I know there is not.

using more of C++ you could put units into a container rather than messing around with arrays. You can change the typedef to use a more appropriate container if necessary.

        typedef vector<Unit> Units;Units myUnits;myUnits.push_back(Unit(1, 3, 3, 5));myUnits.push_back(Unit(7, 6, 1, 2));myUnits.push_back(Unit(4, 4, 15, 3));//etc.    


or you could define default objects and use them

  static const Unit marine = Unit(1,3,3,5);static const Unit sweeper = Unit(7,6,1,2);Units myUnits;myUnits.push_back(marine);myUnits.push_back(marine);myUnits.push_back(sweeper);myUnits.push_back(Unit(4, 4, 15, 3));      


[edited by - petewood on March 17, 2003 9:58:36 AM]

This topic is closed to new replies.

Advertisement