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

G

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);
}
 
J

John Harrison

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
 
J

Jacques Labuschagne

David said:
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.
 
R

roman ziak

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
 
J

JoeRB

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;
}
};
 
S

Serge Paccalin

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
 
O

Old Wolf

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;
};
 
K

Kleidemos

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top