C/C++ Simple C++ method question (overloading?)

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    C++ Method
AI Thread Summary
The discussion revolves around the challenge of modifying class variables in a method designed to accept multiple parameters. The initial question seeks a way to reuse a method that assigns values to five class variables while only changing one variable at a time. The conversation highlights method overloading as a potential solution, allowing methods with the same name but different parameters. A suggested approach involves using a single method that takes a pointer to the class variable and a new value, enabling the modification of individual variables without creating separate methods for each. This method, `changeVar(int *pl, int r)`, allows for efficient updates by directly referencing the class variables. Overall, the focus is on optimizing method design to enhance code reusability and maintainability.
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;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
25
Views
2K
Replies
34
Views
4K
Replies
23
Views
2K
Replies
36
Views
3K
Replies
31
Views
3K
Replies
9
Views
2K
Replies
6
Views
3K
Replies
18
Views
3K
Back
Top