Christopher Peterson
Christopher Peterson

Reputation: 1027

Generic first argument C++ function

I have a lot of setters. Is there a generic way to get the first argument of each setter without spelling the name of each different setters argument? For example:

void
Class::setSelectedEntryIndex(int newSelectedEntryIndex) {
    m_log(ExEr) << "first arg: " << newSelectedEntryIndex << std::endl;
    m_selectedEntryIndex = newSelectedEntryIndex;
    emit selectedEntryIndexChanged();
}

replaced with:

void
Class::setSelectedEntryIndex(int newSelectedEntryIndex) {
    m_log(ExEr) << "first arg: " << this->firstArg << std::endl;   // Change here
    m_selectedEntryIndex = newSelectedEntryIndex;
    emit selectedEntryIndexChanged();
}

?

Upvotes: 0

Views: 142

Answers (1)

bitmask
bitmask

Reputation: 34628

Not the way you propose. You could change the parameter-list into an std::tuple but since you're dealing with setters I assume your parameter-list have one parameter each.

Your best shot to automate (what I think you want to do) is to simply name all parameters the same in your function definition. You can still give them descriptive names in the declaration, since C++ doesn't care about that, anyway.

.h

class MyClass {
  void setTemperature(int newTemperature);
  // or
  void setTemperature(int); // <- people sometimes find this less self-documentative
};

.cpp

void MyClass::setTemperature(int param1) {
  m_log(ExEr) << "first arg: **" << param1 << std::endl;
  /* ... */
}

Upvotes: 1

Related Questions