Hi
im using switch to access some member functions through object s1 of that class

switch(choice)
    {
      case 1: s1.Now();  break;    
      case 2: s1.create();  break;
      case 3: s1.display();  break;
      case 4: s1.change();  break;}

Is there a way to change/use only a part of a function? For example for case 2 i create objects, but for case 4 i want to modify the created objects in function create() .
Im trying it this way; i can change case 4 to

case 4: s1.create();

to access create() function, but i only want to access the part that does the changing, not the rest of the function. How can i do that?

Or is there any other way? Can a function inherit from another function?

It sounds like you want to refactor the changing part of the create member function into the change member function. Then in the create member function, you call change:

class Test {
public:
  void create()
  {
    // Create stuff
    change();
  }

  void change()
  {
    // Change stuff
  }
};

And your switch remains the same.

>Is there a way to change/use only a part of a function?
To answer your question, yes there are ways, but no there aren't any good ones. For example, you could add a boolean flag parameter that says whether to create or not. But that changes the meaning of the function and gives it two jobs rather than one. Bad idea.

>To answer your question, yes there are ways, but no there aren't any good ones. For example, you could add a boolean flag parameter that says whether to create or not. But that changes the meaning of the function and gives it two jobs rather than one. Bad idea.

not if by default it equals to false.

>not if by default it equals to false.
You can default it to whatever you want, but the fact still remains that you're using the value of an argument to completely change the meaning of a function. That's an example of bad design. Take realloc for example:

q = realloc ( p, size );

What does this line of code do?

Thanks for the help, your example gave me another idea on how to do it.

>To answer your question.....
yes i was thinking that myself, it could get ugly, but was not sure. I added a boolean parameter but in another way

class Test {
public:
  void create()
  {
    // Create stuff
    if (condition==true); //change
  }
 
  void change()
  {
    condition=true; // true when call to this functions is made
  }
};

Thanks!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.