使用一个类中的函数更改另一个类c+中变量的值+; 是否可以使用类中的函数来更改另一类C++中变量的值。我想使用manger类来更改events类中的price或函数中的price #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; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 班级经理{ 公众: void Changevalue(){ //在事件类的某些位置更改变量的值。 } }; 班级活动{ //变数 无效价格(){ 国际价格; } }; int main(){ a经理; 事件b; a、 Changevalue();//用于更改events类中变量的值 返回0; }

使用一个类中的函数更改另一个类c+中变量的值+; 是否可以使用类中的函数来更改另一类C++中变量的值。我想使用manger类来更改events类中的price或函数中的price #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; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 班级经理{ 公众: void Changevalue(){ //在事件类的某些位置更改变量的值。 } }; 班级活动{ //变数 无效价格(){ 国际价格; } }; int main(){ a经理; 事件b; a、 Changevalue();//用于更改events类中变量的值 返回0; },c++,C++,您可能会将事件作为参数传递给ChangeValue函数。然后,您可以使用该类提供的任何getter或setter方法。您可能会将事件作为参数传递给ChangeValue函数。然后,您可以使用该类提供的任何getter或setter方法。您可能会将事件作为参数传递给ChangeValue函数。然后,您可以使用该类提供的任何getter或setter方法。您可能会将事件作为参数传递给ChangeValue函数。然后可以使用该类提供的任何getter或setter方法。是 如果事件类中的变量是公共静态

您可能会将事件作为参数传递给ChangeValue函数。然后,您可以使用该类提供的任何getter或setter方法。

您可能会将事件作为参数传递给ChangeValue函数。然后,您可以使用该类提供的任何getter或setter方法。

您可能会将事件作为参数传递给ChangeValue函数。然后,您可以使用该类提供的任何getter或setter方法。

您可能会将事件作为参数传递给ChangeValue函数。然后可以使用该类提供的任何getter或setter方法。

  • 如果
    事件
    类中的变量是
    公共静态
    ,则可以直接访问它

    void Changevalue()
    {
      Event::someVar = someValue; 
    }
    
  • 如果它不是
    静态的
    ,而是
    公共的
    ,则需要一个
    事件
    的对象,您可以更改该对象的值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 如果既不是
    静态
    也不是
    公共
    ,则需要
    事件
    的对象和
    事件
    中的
    公共
    方法来更改值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 其中
    事件
    类如下

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

  • 如果
    事件
    类中的变量是
    公共静态
    ,则可以直接访问它

    void Changevalue()
    {
      Event::someVar = someValue; 
    }
    
  • 如果它不是
    静态的
    ,而是
    公共的
    ,则需要一个
    事件
    的对象,您可以更改该对象的值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 如果既不是
    静态
    也不是
    公共
    ,则需要
    事件
    的对象和
    事件
    中的
    公共
    方法来更改值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 其中
    事件
    类如下

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

  • 如果
    事件
    类中的变量是
    公共静态
    ,则可以直接访问它

    void Changevalue()
    {
      Event::someVar = someValue; 
    }
    
  • 如果它不是
    静态的
    ,而是
    公共的
    ,则需要一个
    事件
    的对象,您可以更改该对象的值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 如果既不是
    静态
    也不是
    公共
    ,则需要
    事件
    的对象和
    事件
    中的
    公共
    方法来更改值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 其中
    事件
    类如下

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

  • 如果
    事件
    类中的变量是
    公共静态
    ,则可以直接访问它

    void Changevalue()
    {
      Event::someVar = someValue; 
    }
    
  • 如果它不是
    静态的
    ,而是
    公共的
    ,则需要一个
    事件
    的对象,您可以更改该对象的值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 如果既不是
    静态
    也不是
    公共
    ,则需要
    事件
    的对象和
    事件
    中的
    公共
    方法来更改值

    void Changevalue(Event& evt)
    {
      evt.someVar = someValue;
    }
    
    void Changevalue(Event& evt)
    {
      evt.ChangeSomeValue( someValue); //this is the best approach in OOP world
    }
    
  • 其中
    事件
    类如下

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

    管理器
    事件
    作为参数:

    Event e;
    Manager m(e);
    

    然后,在
    Event
    上添加一个成员函数,
    Manager
    可以调用该函数来更改其值。

    Manager
    Event
    作为参数:

    Event e;
    Manager m(e);
    

    然后,在
    Event
    上添加一个成员函数,
    Manager
    可以调用该函数来更改其值。

    Manager
    Event
    作为参数:

    Event e;
    Manager m(e);
    

    然后,在
    Event
    上添加一个成员函数,
    Manager
    可以调用该函数来更改其值。

    Manager
    Event
    作为参数:

    Event e;
    Manager m(e);
    

    然后,在
    Event
    上添加一个成员函数,
    Manager
    可以调用该函数来更改其值。

    您可以将类管理器添加为类事件的好友类。 只需将成员添加到类事件:

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

    但是price也应该是类event的成员,否则不能更改函数中变量的值。因为它是一个局部变量。

    您可以将类管理器添加为类事件的友元类。 只需将成员添加到类事件:

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

    但是price也应该是类event的成员,否则不能更改函数中变量的值。因为它是一个局部变量。

    您可以将类管理器添加为类事件的友元类。 只需将成员添加到类事件:

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

    但是price也应该是类event的成员,否则不能更改函数中变量的值。因为它是一个局部变量。

    您可以将类管理器添加为类事件的友元类。 只需将成员添加到类事件:

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

    但是price也应该是类event的成员,否则不能更改函数中变量的值。因为它是一个局部变量。

    我根据代码的当前结构,对您要做的事情做一些假设。例如,我假设您希望设置对象的变量,而不是类的变量,因为您声明了对象

    #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;
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    班级活动{
    国际价格;
    公众:
    无效设定价格(INTP){
    价格=p;
    }
    };
    班级经理{
    事件*事件;
    公众:
    经理(事件*e):事件(e){}
    void ChangeValue(){
    //在事件类的某些位置更改变量的值。
    事件->设置价格(5);
    }
    };
    int main(){
    事件b;
    经理a&b;
    a、 ChangeValue();//用于更改