简体   繁体   中英

Using a function in a class to change the value of a variable in another class c++

Is it possible to Use a function in a class to change the value of a variable in another class c++. I want to use the manger class to change the price in a events class or the price in the function.

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <sstream>

using namespace std;

class Manager {
public:
    void Changevalue() {
        //Changes the value of a variable some where in events class.
    }
};

class Event {
    //variable
    void price() {
        int price;
    }
};

int main() {
    Manager a;
    Event b;
    a.Changevalue();// use to change the value of a variable in the events class

    return 0;
}

Yes.

  1. If the variable in Event class is public static , you can directly access it

     void Changevalue() { Event::someVar = someValue; } 
  2. If it is not static but public , you will need an object of Event of which you change the value

     void Changevalue(Event& evt) { evt.someVar = someValue; } 
  3. If it is neither static nor public , you will need an object of Event and a public method in Event through which you change the value

     void Changevalue(Event& evt) { evt.ChangeSomeValue( someValue); //this is the best approach in OOP world } 

Where Event class will be like

class Event {

  pulbic:
     void ChangeSomeValue( someType someValue);
  //other code
};

You can add class Manager as a friend class of the class event. Just add a member to class event:

class Event
{
public:
  friend class Manager;
  int price;
  void price()
  {

  }

But the price should be a member of class event too, or you can not change the value of a variable in a function. Because it's a local variable.

You can likely pass the Event to the ChangeValue function as an argument. You could then use any getter or setter methods the class provides.

Have Manager take Event as an argument:

Event e;
Manager m(e);

Then, add a member function on Event that Manager can call to change its value.

I am making a few assumptions about what you are trying to do based on the current structure of your code. For example, I assume that you want to set variables of objects, and not of classes, because you declared objects.

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <sstream>

using namespace std;

class Event {
    int price;
    public:
    void setPrice(int p) {
        price = p;
    }
};

class Manager {
    Event *event;
    public:
    Manager(Event * e) : event(e) {}
    void ChangeValue() {
        //Changes the value of a variable some where in events class.
        event->setPrice(5);
    }
};


int main() {
    Event b;
    Manager a(&b);
    a.ChangeValue();// use to change the value of a variable in the events class

    return 0;
}

Q: Is it possible to Use a function in a class to change the value of a variable in another class?

A: Sure. There are lots of ways. For example:

1) You can simply make "price" a public member of "Event" - so anybody can change it, at any time. This is usually Bad...

2) You can make Manager.ChangeValue() a friend of Event:

http://www.cprogramming.com/tutorial/friends.html

3) Or, arguably best, you can create a getter/setter method in "Event"

CAVEAT:

// Questionable
class Event {
public:
    void price() {
        int myPrice;  // The scope of this variable exists *ONLY INSIDE THE FUNCTION*
    }
};

// Better
class Event
private:
  int price;
public:
  int getPrice() { return price; }
  void setPrice (int p) { price = p; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM