如何在C ++中从另一个类调用一个类的函数 [英] How do I call function of one class from another class in C++

查看:906
本文介绍了如何在C ++中从另一个类调用一个类的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

using namespace std;

class Enemy {
public:
    void setAttackDamage(int x){
        attackDamage = x;
    }
    
    void setHealth(int y){
        health = y;
    }
    
    void lowerHealth(int z){
        health -= z;
    }
    
    virtual void attack() = 0;
    
protected:
    int attackDamage;
    int health;
    
};

class Ninja : public Enemy {
public:
    void attack(){
        cout << "Ninja attacked with " << attackDamage << " !" << endl;
        Samurai::lowerHealth(attackDamage);
    }
    
};

class Samurai : public Enemy {
public:
    void attack(){
        cout << "Samurai attacked with " << attackDamage << " !" << endl;
        Ninja::lowerHealth(attackDamage);
    }
    
};

int main() {
    
    Ninja n;
    Samurai s;
    
    Enemy *e1 = &n;
    Enemy *e2 = &s;
    
    e1->setAttackDamage(5);
    e1->setHealth(100);
    
    e2->setAttackDamage(6);
    e2->setHealth(90);
    
    
    
    return 0;
    
}





i我试图调用



i am trying to call the

lowerHealth()

其他类的功能,反之亦然..任何想法??



我尝试了什么:



i尝试使用范围解析运算符尝试从另一个类调用该函数,但它认为如果我不确定则需要一个对象。请多多谢谢

function of the other class and vice versa .. any ideas ??

What I have tried:

i tried using the scope resolution operator to try and call the function from the other class but it think that requires an object if im not sure. please help many thanks

推荐答案

类的方法只能由类的实例调用,或者方法必须是静态的。如果它是静态的,那么in不能访问需要对象实例的成员,即它只能访问其他静态方法和成员,除非它可以通过指针或全局变量访问实例。



因为它们具有相同的基类,并且方法在基类中,所以只需将指针传递给另一个实例。它需要知道它正在攻击谁?
Methods of a class can only be called by an instance of the class OR the method must be static. If it is static then in can not access members that require an instance of the object ie., it can only access other static methods and members unless it has access to an instance via a pointer or global variable.

Since these have the same base class and the method is in the base class then just pass a pointer to the other instance. It needs to know who it is attacking right?
// I use the p wart to indicate pointer variables.
pe1->attack( pe2 );  // pe1, the ninja is attacking pe2, the samarai


你还没有理解C ++中继承的类概念。函数属于类。阅读此教程以更好地理解它。



You havent understood the class concept of inheritance in C++. A function belongs to a class. Read this tutorial to understand it better.

class Samurai : public Enemy {
public:
    void attack(){
        cout << "Samurai attacked with " << attackDamage << " !" << endl;
        Enemy::lowerHealth(attackDamage);//call from the right parent (you dont need to write it
    }
};

在主要内容中,您可以直接调用函数

And in the main you can call the functions directly

int main() {
    
    Ninja n;
    Samurai s;
    
    n.setAttackDamage(5);
    n.setHealth(100);
    
    s.setAttackDamage(6);
    s.setHealth(90);    
    
    return 0;    
}


这篇关于如何在C ++中从另一个类调用一个类的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆