C++ question...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

C++ question...

jbect
Hi guys,

Still trying to get the JIT back to life, I stumbled upon the following
design :

class
jit_operation
{
public:
   // ...
   const std::string& name (void) const { return mname; }
   void stash_name (const std::string& aname) { mname = aname; }
   // ...
private
   // ...
   std::string mname;
};


Is there some fancy C++ trick hidden there, or would it equivalent to
simply have a public std::string variable "name"?

@++
Julien


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++ question...

Lars Winterfeld-2
Hi,

I think those getter and setter methods are a general concept of object
oriented programming. Maybe some day you will decide that whenever the
name is updated, something else in the class will need updating, too. Or
maybe you later realize that you can trigger a bug when the name is
empty, so you want to enforce a nonzero length. Or maybe - due to some
jit magic - the name is later stored no longer as member but elsewhere,
and you still want to be able to return a reference to the name, etc. So
then, you can just implement that in the getter and setter methods,
instead of browsing through the code again and look at every possible
access of "mname".
(This is general. I haven't looked at the context.)

If this is written in header like in the code snippet, it can easily
optimized away. So it should not cause bad performance.

(If one can be sure that really and forever just data needs to be
stored, use a struct.)

Best wishes,
Lars


Am 02.08.2017 um 12:07 schrieb Julien Bect:

> Hi guys,
>
> Still trying to get the JIT back to life, I stumbled upon the following
> design :
>
> class
> jit_operation
> {
> public:
>    // ...
>    const std::string& name (void) const { return mname; }
>    void stash_name (const std::string& aname) { mname = aname; }
>    // ...
> private
>    // ...
>    std::string mname;
> };
>
>
> Is there some fancy C++ trick hidden there, or would it equivalent to
> simply have a public std::string variable "name"?
>
> @++
> Julien
>
>

Loading...