C ++:是否有可能重载|两个不同枚举的运算符驻留在同一个类中?

时间:2015-09-17 18:44:15

标签: c++ enums operator-overloading operators overloading

procedure TNoScrollDBGrid.WMNCCalcSize(var msg: TMessage);
const
 scrollstyles = WS_VSCROLL or WS_HSCROLL;
var
 style: Integer;
begin
 style := getWindowLong( handle, GWL_STYLE );
 If (style and scrollstyles) <> 0 Then
   SetWindowLong( handle, GWL_STYLE, style and not scrollstyles );
 inherited;
end;

type
  TMyGrid = class(TDBGrid);

procedure TForm1.GetDrawInfo;
var
  DrawInfo : TGridDrawInfo;
  i : Integer;
  Min,
  Max,
  R,
  NewLastCellWidth,
  VisibleDataColumns : Integer;
begin
  TMyGrid(NoScrollDBGrid1).CalcDrawInfo(DrawInfo);

  Min := 0;
  //  calculate the number of visible data columns, allowing for the
  //  indicator column, if there is one.
  VisibleDataColumns := DrawInfo.Horz.GridCellCount - DrawInfo.Horz.FixedCellCount;

  Max := VisibleDataColumns -1;
  if DrawInfo.Horz.FixedCellCount = 0 then
    Dec(Max);
  R := DrawInfo.Horz.FixedBoundary;

  //  Next, sum the widths of the data columns, *except*the right-most one   
  for i := Min to Max - 1 do begin
    Inc(R, NoScrollDBGrid1.Columns[i].Width);
  end;

  if R < DrawInfo.Horz.FullVisBoundary then begin
    //  Work out what the width of the rightmost column needs to be
    //  to fill the grid
    NewLastCellWidth := DrawInfo.Horz.GridExtent - R;
    //  adjust this to allow for the width of the vertical lines between
    // the columns, etc
    NewLastCellWidth := NewLastCellWidth - 1 * (Max +1);
    NoScrollDBGrid1.Columns[Max].Width := NewLastCellWidth;
  end;
  Caption := IntToStr(R);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GetDrawInfo;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j: Integer;
  Field : TStringField;
const
  Fields = 4;
begin
  for i := 1 to Fields do begin
    Field := TStringField.Create(Self);
    Field.Size := 20;
    Field.FieldName := 'Field' + IntToStr(i);
    Field.Name := Field.FieldName;
    Field.FieldKind := fkData;
    Field.DataSet := CDS1;
  end;
  CDS1.CreateDataSet;
  CDS1.Insert;
  for i := 1 to Fields do begin
    CDS1.Fields[i-1].AsString := 'Value: ' + IntToStr(i);
  end;
  CDS1.Post;
  NoScrollDBGrid1 := TNoScrollDBGrid.Create(Self);
  NoScrollDBGrid1.Parent := Self;
  NoScrollDBGrid1.Width := 648;
  NoScrollDBGrid1.DataSource := DataSource1;
end;

我想超载|上面两个枚举上的运算符。有可能吗?

请注意,这里有两个枚举,而不仅仅是一个,我想重载|对于两者。

感谢。

1 个答案:

答案 0 :(得分:4)

是的,就像您可以使用常规功能一样,您可以重载运算符以获取不同类型的参数

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/vehicle_color_cell"
>
<TextView
    android:text="Medium Text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/vehicle_color_textview"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:minHeight="30dp"
    android:gravity="center"
    android:textColor="#FFFFFF"
    />
</RelativeLayout>

Live Example

但要小心使用过时的#include <iostream> struct myClass { enum firstEnum { Value1, Value2, Value3}; enum secondEnum { ValueA, ValueB, ValueC}; friend void operator|(firstEnum L, firstEnum R) { std::cout << "first\n"; } friend void operator|(secondEnum L, secondEnum R) { std::cout << "second\n"; } }; int main() { myClass::Value1 | myClass::Value2; // first myClass::ValueA | myClass::ValueB; // second myClass::Value1 | myClass::ValueA; // prints nothing, converts enums to builtin operator|(int, int) } ,它们会隐式转换为整数类型,以便混合调用enum将调用operator|上的内置operator|。使用C ++ 11 int来获得更多类型安全的行为。

更新:上面的代码也为C ++ 98编译,只是C ++ 11提供了更多类型安全的枚举。