Writing C++ class and member functions

I have the following class and thought that when I call the set command to set a member, I always use value. Would that be fine?


class ModMisfit {

protected:

  Real    dtau;
  Real    mdacc;
  Real    mindist;
  bool    hw;
  Source**    src;

public:

  void  set_integration(
    const Integration  value);

  void  set_dtau(
    const Real  value);

  void  set_mdacc(
    const Real  value);

  void  set_mindist(
    const Real  value);

  void  set_hw(
    const bool  value);

};

void ModMisfit::set_dtau(
  const Real  value) {

  for ( int i = 0; i < nsrcs; i++ ) {
      src->SetdTau( value );
  }

}

inline void ModMisfit::set_mdacc(
  const Real  value ) {

  mdacc = value;

}

inline void ModMisfit::set_mindist(
  const Real  value ) {

  mindist = value;

}

void ModMisfit::set_integration(
  const Integration  value ) {

  for ( int i = 0; i < nsrcs; i++ ) {
      Src->SetIntegration( value );
  }

}

Could you rephrase the question? I don't think that quite came across.

Which set are you talking about anyway? The code is full of them.

I made the code a bit simpler. Hope it helps. I have defined members

dtau, mdacc, mindist, hw, ...

When I create the member functions, I call the input argument the same name, I call value. Originally I had something as shown below:

Do you think my new code would be better as I have lot of names that are the same. How does one usually deal with such instances?

class ModMisfit {

protected:

  int     nsrcs;
  Real    Dtau;
  Real    Mdacc;
  Real    Mindist;
  bool    Hw;
  Source**    Src;

public:

  void  set_dtau(
    const Real  dtau);

  void  set_mdacc(
    const Real  mdacc);

  void  set_mindist(
    const Real  mindist);

  void  set_hw(
    const bool  hw);

};

void ModMisfit::set_dtau(
  const Real  dtau) {

  for ( int i = 0; i < nsrcs; i++ ) {
      Src->SetdTau( dtau );
  }

}

inline void ModMisfit::set_mdacc(
  const Real  mdacc ) {

  Mdacc = mdacc;

}

inline void ModMisfit::set_mindist(
  const Real  mindist ) {

  Mindist = mindist;

}

void ModMisfit::set_integration(
  const Integration  intg ) {

  for ( int i = 0; i < nsrcs; i++ ) {
      Src->SetIntegration( intg );
  }

}

They're not really the same value, some are capitalized, some aren't.

As long as you have a consistent system for telling which is which and stick to it, it should be maintainable.

Looking for a naming convention that resolves the problem of finding reasonable variable names for setter methods and constructors.

---------- Post updated at 05:48 PM ---------- Previous update was at 05:44 PM ----------

Yes, I capitalize member variables, but all inputs to the set functions use the variable name 'value'.

---------- Post updated at 07:39 PM ---------- Previous update was at 05:48 PM ----------

Can one use the same variable name for the class member and the arguments for the setter functions as shown below?

class ModMisfit {

// -- Attributes -----------------------------------------------------------------------------------

protected:

  static const Real    dflt_dtmin;
  static const Real    dflt_sigma;
  static const Real    dflt_kbeta;
  static const double  dflt_dangsh;
  static const bool    dflt_hw;
  // static const char    DelStr[];
  int     NPhases;
  int     nSources;
  double  dangsh;
  int     iterMax;
  Real    mdacc;
  Real    mindist;
  Real    dtmin;
  Real    sigmaz;
  Real    kbeta;
  bool    hw;
  bool    extrap;
  Source**     srcs;
  List<int>    ds;
  List<int>    dp;
  List<Real>   x;
  List<Real>   t1Lst;
  List<Real>   t2Lst;
  List<Phase>  ph;
  Verbose  vblevel;
  // Real data

// -- Operations -----------------------------------------------------------------------------------

public:

  ModMisfit():                         //
    mindist( -1 ),
    mdacc( -1 ),
    itermax( -1 ),
    dangsh( dflt_dangsh ),
    hw( dflt_hw ),
    extrap( true ),
    dtmin( dflt_dtmin ),
    sigmaz( dflt_sigma ),
    kbeta( dflt_kbeta ),
    vblevel( none ) { }

  void  set_param(                      //
    Parsing&  P );

  void  set_data(                       //
    Parsing&  P );

  void  set_integration(                //
    const Integration  intg );

  void  set_dangsh(                     //
    const double  dang );

  void  set_dtau(                       //
    const Real  dtau );

  void  set_mindist(                    //
    const Real  mindist );

  void  set_itermax(                    //
    const int  itermax );

  void  set_hw(                         //
    const bool  hw );

  Real  get_misfit(                     //
    Velmod*  vm );

  Real  get_pdf(                        //
    Velmod*  vm );

protected:

  void  ResetSrc();                    //

};

You can call the functions, variables, and parameters whatever you want, as long as they don't overlap. They can't be all the same because, at best, some won't be available to you inside the function, at worst you'll get compiler errors in some contexts.

Beyond that I still have no idea what you're asking.

I have a member function called mdacc (look at the green variable) , and in the setter function I am using mdacc for the input argument. And in the program, the user specifies mdacc as well, that then sets the class value for mdacc.

I used to have the class member starting with capital letter, whereas all the others are lower case. But left me with some confusion . What do people code in such cases?

class ModMisfit {

protected:

  Real    mdacc;

public:

  void  set_mdacc(
      const Real  mdacc);

};

Like I said, it will make the real variable unavailable inside that function. You could access them with classname::varname, maybe. Still, not a good idea to overlap them like that.