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

时光毁灭记忆、已成空白 提交于 2019-12-30 10:41:56

问题


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;

回答1:


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


来源:https://stackoverflow.com/questions/6443716/equivalent-of-withfrom-pascal-to-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!