标签: rtti

对typeid(T).name()的多次调用是否会返回相同的指针?

在C++中,我可以使用typeid运算符来检索任何多态类的名称:

const char* name = typeid( CMyClass ).name();
Run Code Online (Sandbox Code Playgroud)

const char* 只要存在相应的类,返回的指向的字符串将可用于我的程序.

多次调用会typeid(T).name()返回相同的指针值class T还是允许返回不同的指针?

c++ rtti typeid typeinfo visual-c++

6
推荐指数
1
解决办法
443
查看次数

RTTI TRttiMethod.Invoke,stdcall和const参数的错误

我有RTTI TRttiMethod.Invoke,stdcall和const参数的问题:

    obj := TClassRecordTest.Create;
    try
      b.a := 10; b.b := 100;

      a.a := 1;  a.b := 2;
      writeln('b.a='+IntToStr(b.a)+' b.b='+IntToStr(b.b));
      writeln;
      writeln('call test1');
      writeln('a.a='+IntToStr(a.a)+' a.b='+IntToStr(a.b));
      r := VToRec(RTTICall(obj, 'Test1', @a, @b));
      writeln('test1 r.a='+IntToStr(r.a)+' r.b='+IntToStr(r.b));

      a.a := 2;  a.b := 3;
      writeln('call test2');
      writeln('a.a='+IntToStr(a.a)+' a.b='+IntToStr(a.b));
      r := VToRec(RTTICall(obj, 'Test2', @a, @b));
      writeln('test3 r.a='+IntToStr(r.a)+' r.b='+IntToStr(r.b));

      a.a := 3;  a.b := 4;
      writeln('call test3');
      writeln('a.a='+IntToStr(a.a)+' a.b='+IntToStr(a.b));
      r := VToRec(RTTICall(obj, 'Test3', @a, @b));
      writeln('test3 r.a='+IntToStr(r.a)+' r.b='+IntToStr(r.b));

      a.a := 4;  a.b := 5;
      writeln('call test4'); …
Run Code Online (Sandbox Code Playgroud)

delphi rtti delphi-xe

6
推荐指数
1
解决办法
1216
查看次数

使用RTTI访问严格的私有字段

考虑这个简单的代码

{$APPTYPE CONSOLE}

uses
  Rtti,
  SysUtils;

type
  {$M+}
  TFoo = class
  strict private
    class var Field1 : Integer;
    field2 :  Integer;
  private
    field3 :  Integer;
    class var Field4 : Integer;
  end;


Var
    ctx : TRttiContext;
    f   : TRttiField;
begin
  try
    ctx:=TRttiContext.Create;

    for f in ctx.GetType(TFoo).GetFields do
     Writeln(f.Name);


    Writeln('Done');
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
Run Code Online (Sandbox Code Playgroud)

运行此时,仅field3列出该列表.似乎哪个RTTI不支持字段,strict private或者class var,问题是Is possible access a strict private field of a delphi class …

delphi rtti delphi-xe

6
推荐指数
1
解决办法
2171
查看次数

使用boost :: iostreams时找不到警告消息RTTI符号

我使用Boost :: iostreams同时写入我的控制台和文件.当我使用eclipse进行调试时(当然使用gdb),我会收到一条警告,说明我在Boost :: iostreams中使用的某个类没有找到RTTI符号.

这是重现问题的最小代码.

#ifndef BOOST_IO_STREAM_H_
#define BOOST_IO_STREAM_H_

#include <fstream>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
using boost::iostreams::tee_device;
using boost::iostreams::stream;

typedef tee_device<std::ostream, std::ofstream> TeeDevice;
typedef stream<TeeDevice> TeeStream;

#endif /* BOOST_IO_STREAM_H_ */

int
main()
{

  /* A config file to output experiment details */
  std::string self_filename = "./experimentconfig.txt";
  std::ofstream fconfig(self_filename.c_str());
  TeeDevice my_tee(std::cout, fconfig);
  TeeStream cool_cout(my_tee);

  cool_cout << "Output to file and console during experiment run" << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我TeeStream cool_cout(my_tee);在调试期间越线时,我收到以下警告:

warning: RTTI symbol not found for class …
Run Code Online (Sandbox Code Playgroud)

gdb eclipse-cdt rtti debug-symbols boost-iostreams

6
推荐指数
1
解决办法
1万
查看次数

Java中RTTI与反射的区别

我的问题是在运行时如何加载类信息?

有人打电话时instanceof是考虑RTTI还是反思?还是取决于实际情况?

java reflection terminology instanceof rtti

6
推荐指数
1
解决办法
8238
查看次数

是否在运行时或编译时评估了typeid(T)?

我无法在任何地方找到这个看似简单的问题的答案.

以下C++函数是否使用RTTI?它当然不必,但我想知道是否有保证在编译时确定typeid.

template <typename T>
const char *getName()
{
   return typeid(T).name();   // Resolved at compile time?
}
Run Code Online (Sandbox Code Playgroud)

c++ templates rtti

6
推荐指数
2
解决办法
2029
查看次数

如何在Delphi中在运行时创建自定义属性并将其附加到字段

是否可以以及如何在运行时创建自定义属性并将其附加到字段?

uses
  System.SysUtils,
  System.Classes,
  System.Rtti;

type
  MyAttribute = class(TCustomAttribute)
  private
    fCaption: string;
  public
    constructor Create(const aCaption: string);
    property Caption: string read fCaption write fCaption;
  end;

  TFoo = class(TPersistent)
  public
    [MyAttribute('Title')]
    Bar: string;
    Other: string;
  end;

constructor MyAttribute.Create(const aCaption: string);
begin
  fCaption := aCaption;
end;

procedure CreateAttributes(Typ: TRttiType);
var
  Field: TRttiField;
  MyAttr: MyAttribute;
begin
  for Field in Typ.GetFields do
    begin
      if Length(Field.GetAttributes) = 0 then
        begin
          MyAttr := MyAttribute.Create('Empty');
          // how to attach created attribute to Field ???
        end;
    end;
end; …
Run Code Online (Sandbox Code Playgroud)

delphi attributes rtti

6
推荐指数
1
解决办法
1205
查看次数

如何正确使用TRttiMethod.Invoke中的字符串作为参数?

我正在尝试使用RTTI使用Text-property来概括可视组件的内容验证,但是当我尝试将字符串值传递给TRttiMethod.Invoke时,我收到消息"Invalid Typecast".(实际上是"UngültigeTypumwandlung",但我猜,这是一个合适的翻译.)

假设所有传递的对象都是完美的,下面的代码将被删除所有安全措施,断言等.

procedure ValidateTextFieldAndSetFocus(const Field: TObject; const Validator: TObject; const errorStates: array of TStringValidationResult; const sErrorMessage: string);
var
  context  : TRttiContext;
  objField : TRttiType;
  objValid : TRttiType;
  prop     : TRttiProperty;
  execute  : TRttiMethod;
  I        : Integer;
  validResult : TStringValidationResult;
  value    : TValue;
begin
  context  := TRttiContext.Create;
  objField := context.GetType(Field.ClassInfo);
  objValid := context.GetType(Validator.ClassInfo);
  prop     := objField.GetProperty('Text');
  value    := prop.GetValue(Field);
  execute  := objValid.GetMethod('Execute');
  for I := 0 to High(errorStates) do
    if execute.Invoke(Validator,[value]).TryAsType<TStringValidationResult>(validResult) then
      if validResult = errorStates[I] then
      begin
        SetFocusIfCan(Field);
        raise …
Run Code Online (Sandbox Code Playgroud)

delphi rtti delphi-xe3

6
推荐指数
1
解决办法
1011
查看次数

遍历继承层次结构的静态强制转换与动态强制转换

我看过一本关于C++的书,提到使用静态转换导航继承层次结构比使用动态转换更有效.

例:

#include <iostream>
#include <typeinfo>

using namespace std;

class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};

int main() {
    Circle c;

    Shape* s = &c; // Upcast: normal and OK

    // More explicit but unnecessary:
    s = static_cast<Shape*>(&c);
    // (Since upcasting is such a safe and common
    // operation, the cast becomes cluttering)

    Circle* cp = 0;
    Square* sp = 0;

    // Static Navigation …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast casting rtti static-cast

5
推荐指数
3
解决办法
6789
查看次数

我可以从字符串中获取PTypeInfo吗?

这可能是一个"不",但有什么方法可以使用Delphi的RTTI(旧学校或2010扩展RTTI)传入一个包含类型名称的字符串,特别是枚举的名称类型,并让它给我这种类型的PTypeInfo?我查看了RTTI.pas和TypInfo.pas,我没有看到任何可以做到这一点的函数,但我可能错过了一些东西.

我在找什么:

var
  info: PTypeInfo;
begin
  info := GetTypeInfoFromName('TComponentStyle');
end;
Run Code Online (Sandbox Code Playgroud)

或类似的东西.事实是,枚举类型的名称将被传入; 在编译时不知道.

delphi rtti

5
推荐指数
1
解决办法
851
查看次数