开发者

Equivalent of with(from Pascal) to C/C++

开发者 https://www.devze.com 2023-03-14 22:09 出处:网络
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

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;


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";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号