Simple C++ method question (overloading?)

  • Context: C/C++ 
  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    C++ Method
Click For Summary

Discussion Overview

The discussion revolves around method overloading in C++ and the possibility of reusing a method to change class variables without creating multiple methods for each variable. Participants explore different approaches to achieve this, including passing pointers and using overloaded methods.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant asks if a method can be reused to change a single class variable while keeping the same method name for changing multiple variables.
  • Another participant confirms that methods can share the same name through overloading, but emphasizes that they remain distinct methods.
  • A different approach is suggested where a single method takes a pointer to a class variable and a new value, allowing for the modification of any class variable without multiple methods.
  • One participant proposes multiple overloaded methods with different signatures for changing one or more variables, but this raises questions about practicality.
  • There is a suggestion to use a pointer-based method to change class variables, which some participants express uncertainty about in terms of its utility.

Areas of Agreement / Disagreement

Participants generally agree that overloading is possible, but there is no consensus on the best approach to implement the desired functionality. Multiple competing views on method design and utility remain present.

Contextual Notes

Some participants express uncertainty about the effectiveness of the proposed solutions and the implications of using pointers for variable modification.

zeion
Messages
455
Reaction score
1
Hi,

If I have a method that takes in say 5 new variables and assigns 5 class variables to these new values, and I only wanted to change one of these variables, is it possible to reuse this method and only have it take in say 1 new variable and change one class variable without having to write different methods for changing each individual variable?

ie.

class testClass
{
void changeFive (int first, second, third, fourth, fifth)
{
testClassFirst = first;
testClassSecond = second;
...
}

void changeOne (int first)
{
testClassFirst = first;
}
}
 
Technology news on Phys.org
Two different methods can share the same name (overloading), but other than a common name, they are different methods.
 
Last edited:
zeion said:
Hi,

If I have a method that takes in say 5 new variables and assigns 5 class variables to these new values, and I only wanted to change one of these variables, is it possible to reuse this method and only have it take in say 1 new variable and change one class variable without having to write different methods for changing each individual variable?

ie.
Code:
class testClass
{
void changeFive (int first, second, third, fourth, fifth)
{
   testClassFirst = first;
   testClassSecond = second;
   ...
}

void changeOne (int first)
{
   testClassFirst = first;
}
}
Assuming the 5 class variables are all int, this should work.

Code:
void changeVar(int *pl, int r)
{
       *pl = r;
}

You would call it as
Code:
changeVar(&testClassFirst, first);
changeVar(&testClassSecond, second);
etc

Though I am not sure how this helps you.
 
Last edited:
You can write it like this

class testClass
{
void changeFive (int first, second, third, fourth, fifth)
{
testClassFirst = first;
testClassSecond = second;
...
}

void changeFive (int first)
{
testClassFirst = first;
}

void changeFive (int first, int second)
{
testClassFirst = first;
}

void changeOne (int first)
{
testClassFirst = first;
}

void changeOne (int first)
{
testClassFirst = first;
}

phiby's suggestion of using the following is where I would go
void changeVar(int *pl, int r)
{
*pl = r;
}
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
Replies
5
Views
6K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K