简体   繁体   English

为什么指针不写类的地址?

[英]Why doesn't the pointer write the address of the class?

I have an abstract class "Mark" and it has a child class "Int_num".我有一个抽象类“Mark”,它有一个子类“Int_num”。 I also have a "Subject" class.我也有一个“主题”课程。 I want a pointer to the address in the memory of the "Mark" class to be written to the "mark" parameter when calling its constructor.我希望在调用其构造函数时将指向“Mark”类内存中地址的指针写入“mark”参数。 What should I do to make the mark pointer point to the "Mark" class?" occurred, after the compiler complaint about "expression must have class type" or something like that in mark.print_mark()?我应该怎么做才能使标记指针指向“Mark”类?”发生在编译器抱怨“表达式必须具有类类型”或 mark.print_mark() 中的类似内容之后?

class Mark {
private:
    int mark;
public:
    virtual void change_mark(int);
    virtual void print_mark();
    virtual int return_mark();
};

class Int_mark : public Mark {
private:
    int mark;
public:
    Int_mark();
    Int_mark(int);
    ~Int_mark();

    void change_mark(int = 0);
    void print_mark() const;
    int return_mark() const;
};

Int_mark::Int_mark() {
    std::string str_mark;
    std::cout << "New mark: ";
    std::cin.ignore();
    std::getline(std::cin, str_mark);

    str_mark = ltrim(rtrim(str_mark));
    int new_mark;
    try {
        new_mark = stoi(str_mark);
    } catch(...) {
        std::cout <<"wq";
        mark = 1;
        return ;
    }

    try {
        if((new_mark < 1) || (new_mark > 5))
            throw 1;
        else
            mark = new_mark;
    } catch(int a) {
        std::cout << "qw" << std::endl;
        mark = 1;
    }
}
void Int_mark::print_mark() const {
    std::cout << "Mark: " << mark << std::endl;
}

Subject主题

#include "Mark.h"
#include <string>
#include <vector>

class Subject {
private:
    std::string name_subject;
    std::string type_subject;
    unsigned hour_subject = 0;
    void *mark = nullptr;
public:
    Subject();
    Subject(std::string, int);
    Subject(std::string, bool);
    ~Subject();

    void change_mark(unsigned);
    void change_mark(bool);
    void rename_subj(std::string);
    void add_hour(unsigned);
};

Subject::Subject() {
    std::string name_sub;
    std::cout << "Введите название предмета: ";
    getline(std::cin, name_sub);
    name_sub = split_string(name_sub);
    name_subject = name_sub;
    int select = 2;

    if(select == 1) {
        type_subject = "Bool";
        //mark = new Bool_mark();
    } else {
        type_subject = "Int";    
        mark = new Int_mark();   


//What should I do to make the mark pointer point to the "Mark" class?
        mark.print_mark();

}
}

main主要的

#include "subject/Subject.h"

using namespace std;

int main() {
    Subject q;
}

What am I doing wrong?我究竟做错了什么? How should I do this?我该怎么做?

The pointer mark is of type void * .指针mark的类型为void * You could cast it with你可以用

static_cast<Int_mark*>(mark)

and call the function with并调用函数

static_cast<Int_mark*>(mark)->print_mark();

But usually in OOP mark would be a pointer to the base class但通常在 OOP 中mark会是指向基类的指针

Mark *mark = nullptr;

Now you can check for errors with现在您可以检查错误

mark = new Int_mark();   

auto *m = dynamic_cast<Int_mark*>(mark);

if (m)
    m->print_mark();

Remember the virtual destructor in the base class记住基类中的虚拟析构函数

virtual ~Mark();

When to use virtual destructors? 何时使用虚拟析构函数?

Here is a fixed version of your code:这是您的代码的固定版本:

#include <iostream>
#include <string>
#include <vector>

class Mark {
public:
    virtual ~Mark() = default;

    //virtual void change_mark(int) = 0;
    virtual void print_mark() const = 0;
    //virtual int return_mark() const = 0;
};

class Int_mark : public Mark {
private:
    int mark;
public:
    Int_mark();
    Int_mark(int);
    ~Int_mark() override = default;

    //void change_mark(int = 0) override;
    void print_mark() const override;
    //int return_mark() const override;
};

Int_mark::Int_mark() {
    std::string str_mark;
    std::cout << "New mark: ";
    std::cin.ignore();
    std::getline(std::cin, str_mark);

    //str_mark = ltrim(rtrim(str_mark));
    int new_mark;
    try {
        new_mark = stoi(str_mark);
    } catch(...) {
        std::cout <<"wq";
        mark = 1;
        return ;
    }

    try {
        if((new_mark < 1) || (new_mark > 5))
            throw 1;
        else
            mark = new_mark;
    } catch(int a) {
        std::cout << "qw" << std::endl;
        mark = 1;
    }
}
void Int_mark::print_mark() const {
    std::cout << "Mark: " << mark << std::endl;
}

class Subject {
private:
    std::string name_subject;
    std::string type_subject;
    unsigned hour_subject = 0;
    Mark *mark = nullptr;
public:
    Subject();
    Subject(std::string, int);
    Subject(std::string, bool);
    ~Subject();

    void change_mark(unsigned);
    void change_mark(bool);
    void rename_subj(std::string);
    void add_hour(unsigned);
};

Subject::Subject() {
    std::string name_sub;
    std::cout << "Введите название предмета: ";
    getline(std::cin, name_sub);
    //name_sub = split_string(name_sub);
    name_subject = name_sub;
    int select = 2;

    if(select == 1) {
        type_subject = "Bool";
        //mark = new Bool_mark();
    } else {
        type_subject = "Int";    
        mark = new Int_mark();   

        auto *m = dynamic_cast<Int_mark*>(mark);
        if (m)
            m->print_mark();
    }
}

Subject::~Subject() {
    delete mark;
}

int main() {
    Subject q;
}

Since I did not correctly understand the question in the first place, here a way how you can call the member function of base class Mark by object of derived class Int_Mark :由于我一开始没有正确理解这个问题,这里有一个方法如何通过派生类Int_Mark对象调用基类Mark的成员函数:

Int_mark *mark = new Int_mark();
mark->print_mark();       // calls member of the class Int_mark
mark->Mark::print_mark(); // calls member of the class Mark

Make sure that Mark::print_mark() is also defined and not just Int_mark::print_mark()确保还定义了Mark::print_mark()而不仅仅是Int_mark::print_mark()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个类没有隐式转换为指针? - Why doesn't this class implicitly convert to a pointer? 为什么用指针调用GetWindowRect会导致异常,但地址却没有 - Why does calling GetWindowRect with pointer cause exception but address doesn't 如果涉及抽象类,为什么指向派生的指针和指向基的指针不指向相同的地址? - Why doesn't a pointer to derived and a pointer to base point to the same address if abstract classes are involved? 为什么不使用引用地址指针编译 C++ ? - Why doesn't make work on a C++ compile with a reference address pointer? 为什么我不能显示指针的地址? - Why can't I display the address of a pointer? 函数为什么不能正确地转换指针(从基类到派生类) - Why doesn't the function cast a pointer correctly (from base class to derived class) 为什么在尝试 class 成员指针时 SFINAE 技巧不适用于非类类型? - Why SFINAE trick doesn't work for non-class type when tried for class member pointer? 我将一个变量分配给存储在指针地址中的值,那么为什么当初始值改变时它不改变呢? - I assigned a variable to the value of what is stored at a pointer's address, so why doesn't it change when the initial value does? 指针有正确的地址但不显示真实信息 - A pointer have the good address but doesn't show the real information 怪异:指针地址不变,但内容减少了1 - weirdness: pointer address doesn't change but contents off by 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM