Click to See Complete Forum and Search --> : Porting from VB to C++; classes->properties
Yo yo yo
I've been porting one of my programs from VB to C++.
In a VB class I have a property,
Private m_pin(1 To 8)
Private m_value
Private m_port
Public Property Let Pin(iPin, pass)
If pass = True Then
If m_pin(iPin) <> 1 Then
m_value = m_value + (2 ^ (iPin - 1))
m_pin(iPin) = 1
RaiseEvent PortChange(Port, iPin, 1)
End If
ElseIf pass = False Then
If m_pin(iPin) <> 0 Then
m_value = m_value - (2 ^ (iPin - 1))
m_pin(iPin) = 0
RaiseEvent PortChange(Port, iPin, 0)
End If
End If
Out m_port, m_value
End Property
Function Out(m_port as integer, m_value as integer)
'Code Omitted
End Function
Basically when this 'variable' is set, it acts like a function. When I port this to C++ I can't use a function, I need to use a property.
How can this be done in C++??
The VB event,
Public Event PortChange(Port, Pin, Value)
'Code Omitted
Raiseevent PortChange(888, 1, 1)
How can I make a similar event in a c++ class??
bwkaz
05-30-2004, 01:46 PM
You can't make a "property" in C++.
In VB, what looks like a property IS really a function (actually, it's a pair of functions, one to set the value and one to get it). There is no such thing as a "property" in C++.
In Borland C++, you can define something that looks like a property, but it's a proprietary extension of the C++ language, and not interoperable with other compilers. I believe you can do the same thing with MSVC++, but if you can, it's still not going to be compatible with other C++ compilers.
Why do you think you need it to be a property?
As for the event, that doesn't exist in C++ either. You're setting yourself up for a ton of work to try to reimplement events, even with something superficially similar like function pointers -- because with events, you can have any number of "listeners", but to do that with a function pointer, you need a list of subscribers or something very much like it.
Originally posted by bwkaz
You can't make a "property" in C++.
In VB, what looks like a property IS really a function (actually, it's a pair of functions, one to set the value and one to get it). There is no such thing as a "property" in C++.
Simple enough, I'll make due.
Why do you think you need it to be a property?
Its an assignment from my teacher, he only knows VB, and he created a very large independant study unit. It is apart of what I have to complete, but I'll explain this limitation to him.
As for the event, that doesn't exist in C++ either. You're setting yourself up for a ton of work to try to reimplement events, even with something superficially similar like function pointers -- because with events, you can have any number of "listeners", but to do that with a function pointer, you need a list of subscribers or something very much like it.
I'm not farmilliar with what a "listener" is. I'll look it up, but could you give me a simple example (if a simple listener example is possible).
Thanks
bwkaz
05-30-2004, 03:27 PM
A listener is basically the function that handles the event.
If you want to have multiple components handling a single event separately (doing something different in each of them, or each of them doing a part of the whole amount of work that needs to be done), then you will use the fact that events can have multiple listeners. For example, in a generic Person class, I can define this:
Public Event BirthdayHappened(), then in my main window, I can hook up a listener:
Private WithEvents myPerson As Person
Public Property Set Person(New_Person as Person)
Set myPerson = New_Person
End Property
Private Sub myPerson_BirthdayHappened()
// do something
End Sub Then, in another window, I can hook up another listener to do something else (code omitted because it's identical).
If you set the Person property of both windows to the same instance, then that instance's BirthdayHappened event being fired means you need to invoke two different functions, not just one.
sploo22
05-30-2004, 06:06 PM
You can probably save yourself a lot of work by using a system that's already implemented to handle events. The GObject (http://developer.gnome.org/doc/API/2.0/gobject/index.html) library should be installed on most Linux systems already since it comes with GTK. Read the documentation, especially the part about signals.
But as bwkaz said, you might not realize what you're getting into. C++ and C are, and always will be, low-level languages. VB does a lot of stuff behind the scenes that you'll have to take care of yourself in C++.
bwkaz
05-30-2004, 06:21 PM
Like I said above, you can't. At least, not easily.
You might be able to keep a std::vector<> of function pointers, but it wouldn't automatically be type-safe; you'd have to take precautions on both ends to ensure the right values got passed to the event handler. And I don't even think you can take the address of a member function; I think the function has to be static in order for the address-of operator to work properly (part of it has to do with the hidden this pointer that gets passed by the compiler to all C++ class-member functions).
<Edit: As sploo22 said, GObject gets my vote for a good object system too. It's implemented in C, not C++, but that's because C++ is too monstrous for the tastes of the developers at Gtk/glib/Gnome "headquarters". It's also too monstrous for my tastes; it's downright impossible to keep it all in your head at once.>