Archived

This topic is now archived and is closed to further replies.

Daivuk

"with" in vb in C++ ?

Recommended Posts

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++???

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
hum. look interresting...
But, I will keep my old method. Cuz I want to visualize it easisy.. I know its not the proper method and your probably better.

But its easy to read it and change thing :


  
// Les unitée DESCRIPTION

DESC_UNIT[GMUNIT_CHOOSEN_MOBILE_CONSTRUCTOR].Icone = &_texMapEdIcone[18];
DESC_UNIT[GMUNIT_CHOOSEN_MOBILE_CONSTRUCTOR].Life = 100;
DESC_UNIT[GMUNIT_CHOOSEN_MOBILE_CONSTRUCTOR].Shield = 0;
DESC_UNIT[GMUNIT_CHOOSEN_MOBILE_CONSTRUCTOR].SpeedIncomShield = 0;

DESC_UNIT[GMUNIT_PARASITE_CRAWLER].Icone = &_texMapEdIcone[25];
DESC_UNIT[GMUNIT_PARASITE_CRAWLER].Life = 55;
DESC_UNIT[GMUNIT_PARASITE_CRAWLER].Shield = 25;
DESC_UNIT[GMUNIT_PARASITE_CRAWLER].SpeedIncomShield = 1.2f;

etc...

Share this post


Link to post
Share on other sites