3

What is the equivalent of with from Pascal language in C/C++ language?

A with statement is a shorthand for referencing the fields of a record or the fields, properties, and methods of an object.

Example

With (Object) do
begin
   Width:=200;
   Height:=300;
end;

Is Equivalent with:

Object.Width=200;
Object.Height=200;
CharlesB
  • 86,532
  • 28
  • 194
  • 218
user558126
  • 1,303
  • 3
  • 21
  • 42
  • I know that this comment isn't answering author's question and expresses solely my personal opinion, but: `with` proved to be a bad thing in Pascal because there is no means to distinguish between members of `Object` variable and other variables/routines from the scope. Nested `with` turned out to be an absolute nightmare. In Visual Basic `with` members have at least a "dot" prefix, in Pascal - none at all. I decided not to use them at all and prefer to make a one-letter-variable for this use case. – Paul May 04 '21 at 13:52

1 Answers1

4

I don't believe that there is any direct equivalent to that statement in c/c++.

If your objective is to avoid repeatedly typing "Object", then I suppose you could use a reference to map it to a shorter name, such as:

  ClassName& o = Object;
  o.prop1 = "meep";
  o.prop2 = "moop";

But I would personally only use this in cases where "Object" is a complex expression. E.g.:

  ClassName& o = something.getSomeOtherThing().getSomeThirdThing();
  o.prop1 = "meep";
  o.prop2 = "moop";
Edward Loper
  • 15,374
  • 7
  • 43
  • 52