I have two questions one is kind of a newbie question and the other one is a little more advanced. I have been coding C++ off and on for the past couple of years as a hobby and a little for my EE classes. I usually in my class reference all my class members with the keyword "this->" when using them internally in my methods. I always wondered if this was a good practice or not OR os does it only apply to certain situation.

For my advanced question, I have some hardware that I would like it to send me information instead of my polling it. Right now I have it structured to be polled for data using a thread. This data is placed in a circular buffer awaiting further processing by the main thread. I was thinking that I could derive my Hardware class as a Event Source from the gtkmm gui library. When the hardware object thread detects that the hardware has just sent a packet of data over the serial RS-232 connection it will trigger the onEvent callback and then place the data into the circular queue. Should I adjust my hardware interface code to be an event based model or should I leave as the thread deticated polling main loop. I currently use libSerial for my comms.

Recommended Answers

All 3 Replies

I only have an answer to the first question. I ALWAYS reference member variables with this-> . I've found that it makes code much much more readable. If you have a long function, reader immediately know that this is a member variable and don't have to spend time looking around to see if it was passed to the function, declared inside of the function, or a member variable. I don't believe there are any times when using this-> is harmful, but there are certainly times where NOT using it can produce an ambiguous case that will cause you headaches.

Good luck with your second question - I'd actually start a separate thread in the C forum for this question (with a better title :) )

Dave

I don't believe there are any times when using this-> is harmful, but there are certainly times where NOT using it can produce an ambiguous case that will cause you headaches.

Of course, that's assuming you don't use any kind of naming convention. Prefixing/suffixing data members with an underscore or suffixing with _m are two common conventions that are equally unambiguous as less verbose. Prefixing everything with this-> gets ugly fast if you write code of any complexity.

I don't explicitly write this-> unless necessary.
There is no point - it's a considerable extra effort since most of the function code you write is inside one class or another. And there is no real benefit - the only reason you would need to write this-> is because you defined a local variable with the same name as a member. Whenever you see any reason to do that, you're automatically extra careful with that name. If you're unable to keep all local variable names inside one function in your head, your function is too long.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.