3

Possible Duplicates:
Does C++ have “with” keyword like Pascal?
Equivalent of with(from Pascal) to C/C++

It's very convenient to use "with" in Pascal. And how can I do it in C/C++ programming?

The following from Wikipedia shows how the with keyword is used to avoid repeating the name of a record pointer when doing many accesses through it:

new(ptoNode);

...
with ptoNode^ do
begin
  a := 10;
  b := 'A';
  c := nil
end;
Community
  • 1
  • 1
tzwm
  • 499
  • 1
  • 4
  • 12
  • A better question might describe what you're after without depending on any other context. For instance, I haven't written any pascal code since the early 1990s. If you don't use it, you'll lose it. However, I may still be able to answer the question if you actually tell us the behaviour you want, instead of a different languages construct. – jer Dec 21 '11 at 13:45
  • 1
    My Pascal is *very* rusty - can you explain what `with` does there? – Björn Pollex Dec 21 '11 at 13:45
  • 2
    You can't. (And it's a bad practice in Pascal, too, even if it's supported there. It leads to hard to track down scoping issues, and makes code harder to read and maintain.) – Ken White Dec 21 '11 at 13:45
  • I edited the question to include an example of the `with` keyword, from Wikipedia. – unwind Dec 21 '11 at 13:47
  • See this question: http://stackoverflow.com/q/2279180/929510 – Staven Dec 21 '11 at 13:48
  • The question concerns this type of construct: `MyObj := SomeObject.Create; with MyObj do begin SomeProperty := AVal; BVal := SomeFunc(AVal); end;`, where without the `with` it would be `MyObj.SomeProperty := AVal; BVal := MyObj.SomeFunc(AVal);`. It's a sort of shorthand acess to an object instance in a code block. – Ken White Dec 21 '11 at 13:49
  • 1
    In the D programming language, which many software developers see as a modern C++ (and more), you have exactly what you have in Pascal - the with statement. I would say that D's version of the `with` statement is even better as it takes care of three use-cases. More about it here - http://www.dlang.org/statement.html#WithStatement . – DejanLekic Dec 21 '11 at 13:52

5 Answers5

4

You can't do it in C. And I'd say that in C++ as well (though maybe there are some inhumane template metaprogramming experiments :-) )

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • Ohh... you make me want to try with some trick based on the ternary operator (see http://www.artima.com/cppsource/foreach.html ) – Alexandre C. Dec 21 '11 at 14:58
3

If you must, get yourself a helper (pointer) object and "reduce"

some_complicated_expression->a = 0;
some_complicated_expression->b = 42;

to

{ /* new block to delimit scope of `tmp` */
    whatever *tmp = some_complicated_expression;
    tmp->a = 0;
    tmp->b = 42;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
0

In C, you can't since there is no such functionality. You simply have to repeat the pointer name.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

The answer is structs : see this : Equivalent of with(from Pascal) to C/C++

And this : http://en.wikipedia.org/wiki/Comparison_of_Pascal_and_C

Community
  • 1
  • 1
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • 1
    After reading that other question, how do you conclude that the answer is structs? The only place that word even appears is in the question *tag*. That means it's a question *about* structs. – Rob Kennedy Dec 21 '11 at 14:17
  • And the second link only provides: "There is no equivalent feature to `with` in C." Which would have been shorter to type than the link text!! This answer is so bizarre... – SO_fix_the_vote_sorting_bug Mar 28 '22 at 20:33
0

In C, I don't think you can do it.

As long as you're not trying to code Pascal in C++ though you can just use objects which will have the same effect. Call a member on the object that does the work you want and voila, the extra ptr-> goes away for each step done by the object's method!

Mark B
  • 95,107
  • 10
  • 109
  • 188