How to implement "__property" in ANSI C++ ?

Discussion in 'C++' started by Guest, Jul 23, 2004.

  1. Guest

    Guest Guest

    Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    term to dump Borland (not only BCB).
    As a part of my attempt to dump long-loved BCB I'm trying to investigate how
    one can implement "__property" in ANSI C++.

    Would anyone have a solution hot to implement "__property" in ANSI C++ ie.
    how to make following code compile in non-BCB C++ compiler?

    class TMyClass
    {
    private:
    int FMyVariable;
    void __fastcall SetMyVariable(int Value) { FMyVariable = Value;}
    int __fastcall GetMyVariable() { return FMyVariable;}
    public:
    __property int MyVariable = {read=GetMyVariable, write=SetMyVariable};

    };
    TMyClass MyClass;

    void __fastcall TMyForm::Button2Click(TObject *Sender)
    {
    MyClass.MyVariable = 1234;
    Caption = IntToStr(MyClass.MyVariable);
    }
     
    Guest, Jul 23, 2004
    #1
    1. Advertising

  2. <David> wrote in message
    news:4100e2fd$0$25461$...
    > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    > term to dump Borland (not only BCB).
    > As a part of my attempt to dump long-loved BCB I'm trying to investigate

    how
    > one can implement "__property" in ANSI C++.
    >
    > Would anyone have a solution hot to implement "__property" in ANSI C++ ie.
    > how to make following code compile in non-BCB C++ compiler?
    >
    > class TMyClass
    > {
    > private:
    > int FMyVariable;
    > void __fastcall SetMyVariable(int Value) { FMyVariable = Value;}
    > int __fastcall GetMyVariable() { return FMyVariable;}
    > public:
    > __property int MyVariable = {read=GetMyVariable, write=SetMyVariable};
    >
    > };
    > TMyClass MyClass;
    >
    > void __fastcall TMyForm::Button2Click(TObject *Sender)
    > {
    > MyClass.MyVariable = 1234;
    > Caption = IntToStr(MyClass.MyVariable);
    > }


    There is no way to make that code compile in standard C++. Are you asking
    how to rewrite it into standard C++? If so then try this

    class TMyClass
    {
    private:
    int FMyVariable;
    public:
    void SetMyVariable(int Value) { FMyVariable = Value;}
    int GetMyVariable() const { return FMyVariable;}
    };

    TMyClass MyClass;

    void TMyForm::Button2Click(TObject *Sender)
    {
    MyClass.SetMyVariable(1234);
    Caption = IntToStr(MyClass.GetMyVariable());
    }

    john
     
    John Harrison, Jul 23, 2004
    #2
    1. Advertising

  3. David wrote:

    > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    > term to dump Borland (not only BCB).
    > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
    > one can implement "__property" in ANSI C++.
    >
    > Would anyone have a solution hot to implement "__property" in ANSI C++ ie.
    > how to make following code compile in non-BCB C++ compiler?
    >
    > class TMyClass
    > {
    > private:
    > int FMyVariable;
    > void __fastcall SetMyVariable(int Value) { FMyVariable = Value;}
    > int __fastcall GetMyVariable() { return FMyVariable;}
    > public:
    > __property int MyVariable = {read=GetMyVariable, write=SetMyVariable};
    >
    > };
    > TMyClass MyClass;
    >
    > void __fastcall TMyForm::Button2Click(TObject *Sender)
    > {
    > MyClass.MyVariable = 1234;
    > Caption = IntToStr(MyClass.MyVariable);
    > }
    >
    >


    Macros? It would be pretty ugly, and you'd have to modify all the code.
    Probably not worth it.

    class C{
    public:
    DECLARE(int, MyVariable, GetMyVariable, SetMyVariable);
    };

    with
    #define DECLARE(type, name, get, set) \
    type name; \
    type get()const{ return name; } \
    type set(const type& t){ name = t; }

    The other option is a preprocessor that runs over your code converting
    the Borland syntax into real C++. This sounds easy.

    Jacques.
     
    Jacques Labuschagne, Jul 23, 2004
    #3
  4. Guest

    roman ziak Guest

    <David> wrote in message
    news:4100e2fd$0$25461$...
    > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    > term to dump Borland (not only BCB).
    > As a part of my attempt to dump long-loved BCB I'm trying to investigate

    how
    > one can implement "__property" in ANSI C++.
    >
    > Would anyone have a solution hot to implement "__property" in ANSI C++ ie.
    > how to make following code compile in non-BCB C++ compiler?


    There is no way other than get_Xyz and set_Xyz ... which involves rewriting
    the
    entire code.

    Since Borland did not have BCB for others, I assume you use MS platforms.
    Did you consider managed C++ ?
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vclrf__property.asp

    Roman
     
    roman ziak, Jul 23, 2004
    #4
  5. Guest

    JoeRB Guest

    <David> wrote in message news:<4100e2fd$0$25461$>...
    > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    > term to dump Borland (not only BCB).
    > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
    > one can implement "__property" in ANSI C++.
    >
    > Would anyone have a solution hot to implement "__property" in ANSI C++ ie.
    > how to make following code compile in non-BCB C++ compiler?
    >
    > class TMyClass
    > {
    > private:
    > int FMyVariable;
    > void __fastcall SetMyVariable(int Value) { FMyVariable = Value;}
    > int __fastcall GetMyVariable() { return FMyVariable;}
    > public:
    > __property int MyVariable = {read=GetMyVariable, write=SetMyVariable};
    >
    > };
    > TMyClass MyClass;
    >
    > void __fastcall TMyForm::Button2Click(TObject *Sender)
    > {
    > MyClass.MyVariable = 1234;
    > Caption = IntToStr(MyClass.MyVariable);
    > }


    How about using templates? You could do something like this -

    template <typename T, typename C>
    class Property
    {
    typedef T (C::*Get)() const;
    typedef void (C::*Set)(T);
    Get GetFunc_;
    Set SetFunc_;
    C & Class_;
    public:
    Property(Get GetFunc, Set SetFunc, C &Class)
    : GetFunc_(GetFunc), SetFunc_(SetFunc), Class_(Class) {}

    operator T () const
    {
    return (Class_.*GetFunc_)();
    }

    Property<T, C>& operator=(T val)
    {
    (Class_.*SetFunc_)(val);
    return *this;
    }
    };

    class TMyClass
    {
    int FMyVariable;
    public:
    Property<int, TMyClass> MyVariable;
    TMyClass() : MyVariable(GetMyVariable, SetMyVariable, *this)
    {
    }
    int GetMyVariable() const
    {
    return FMyVariable;
    }
    void SetMyVariable(int Value)
    {
    FMyVariable = Value;
    }
    };
     
    JoeRB, Jul 23, 2004
    #5
  6. Le vendredi 23 juillet 2004 à 11:35, David a écrit dans comp.lang.c++ :

    > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    > term to dump Borland (not only BCB).
    > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
    > one can implement "__property" in ANSI C++.
    >
    > Would anyone have a solution hot to implement "__property" in ANSI C++ ie.
    > how to make following code compile in non-BCB C++ compiler?
    >
    > class TMyClass
    > {
    > private:
    > int FMyVariable;
    > void __fastcall SetMyVariable(int Value) { FMyVariable = Value;}
    > int __fastcall GetMyVariable() { return FMyVariable;}
    > public:
    > __property int MyVariable = {read=GetMyVariable, write=SetMyVariable};
    >
    > };
    > TMyClass MyClass;
    >
    > void __fastcall TMyForm::Button2Click(TObject *Sender)
    > {
    > MyClass.MyVariable = 1234;
    > Caption = IntToStr(MyClass.MyVariable);
    > }


    What about the following?

    class Int
    {
    private:
    int IntValue_;
    public:
    Int & operator=(int Value) { IntValue_ = Value; }
    operator int() const { return IntValue_; }
    };

    class TMyClass
    {
    public:
    Int MyVariable;

    };
    TMyClass MyClass;

    void __fastcall TMyForm::Button2Click(TObject *Sender)
    {
    MyClass.MyVariable = 1234;
    Caption = IntToStr(MyClass.MyVariable);
    }

    --
    ___________ 2004-07-26 08:39:13
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
     
    Serge Paccalin, Jul 26, 2004
    #6
  7. Guest

    Old Wolf Guest

    <David> wrote in message news:
    >
    > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
    > one can implement "__property" in ANSI C++.


    The draft standard for C++/CLI includes 'property' with a different
    (and better) syntax. In fact CLI objects are a ripoff of VCL objects.
    You could wait around until either BCB 9 comes out, or some C++/CLI
    compilers come out.

    > Would anyone have a solution hot to implement "__property" in ANSI C++ ie.
    > how to make following code compile in non-BCB C++ compiler?
    >
    > class TMyClass
    > {
    > private:
    > int FMyVariable;
    > void __fastcall SetMyVariable(int Value) { FMyVariable = Value;}
    > int __fastcall GetMyVariable() { return FMyVariable;}
    > public:
    > __property int MyVariable = {read=GetMyVariable, write=SetMyVariable};
    >
    > };


    class MyClass
    {
    public:
    int MyVariable;
    };
     
    Old Wolf, Jul 27, 2004
    #7
  8. Guest

    Kleidemos Guest

    You can try this mothod

    class Test
    {
    private:
    int _data;
    public:
    Test(int data ): _data(data){};
    ~Test(){};
    int& Data(){ return _data; };
    };

    You, now, can both read a value and write a value.
    Test t(10);
    int i = t.Data(); // i == 10
    t.Data() = 15; // i == 15
    i = t.Data();
    --
    Tnk

    Luca "Kleidemos" Francesca

    Un computer a un altro quando si incontrano:
    "Ciao, come ti boota oggi???"
     
    Kleidemos, Aug 31, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    VB code and Sql Server Ansi Padding

    =?Utf-8?B?U2FuZHk=?=, May 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,492
    =?Utf-8?B?U2FuZHk=?=
    May 11, 2005
  2. Keren Yehezkel

    implement set of numbers in ansi c

    Keren Yehezkel, Apr 14, 2004, in forum: C Programming
    Replies:
    3
    Views:
    415
    Malcolm
    Apr 14, 2004
  3. Replies:
    1
    Views:
    499
  4. Replies:
    11
    Views:
    1,102
    Keith Thompson
    Apr 28, 2008
  5. Frank Iannarilli

    pre-ansi to ansi c++ conversion?

    Frank Iannarilli, Jul 21, 2009, in forum: C++
    Replies:
    2
    Views:
    422
Loading...

Share This Page