简体   繁体   English

使用堆栈实现C ++

[英]Using Stack implementation C++

I am making a toy programming language in c++, but i have run into a problem. 我正在用C ++编写玩具编程语言,但是遇到了问题。 I have noticed that in c++ a stack can only store one type of data. 我注意到在c ++中,堆栈只能存储一种类型的数据。 I was wondering if there was an easy way to fix this problem, such as by storing in the stack a byte array of each object. 我想知道是否有解决此问题的简便方法,例如通过在堆栈中存储每个对象的字节数组。 I was wondering if anyone knows how the jvm overcomes this issue. 我想知道是否有人知道jvm如何解决这个问题。 The types i would need to store on the stack would be char, short, int, float, double, strings, arrays, and references to objects. 我需要在堆栈上存储的类型将是char,short,int,float,double,string,array和对对象的引用。 I understand that the jvm stack might be more of an abstraction, but if it is i would still like to know how they have accomplished it. 我知道jvm堆栈可能更像是一个抽象,但是如果是这样,我仍然想知道他们是如何实现的。 If it makes any difference, i am only planning to target windows computers. 如果有什么不同,我只打算针对Windows计算机。

You know C++ has support for inheritance and polymorphism, right? 您知道C ++支持继承和多态吗? A far easier way to do this is to derive all your tokens from a common base class, and make a stack of Base * objects, for instance: 一种更简单的方法是从一个公共基类派生所有标记,并生成一堆Base *对象,例如:

#include <iostream>
#include <string>
#include <stack>
#include <memory>

class base {
    public:
        virtual void print_token() = 0;
        virtual ~base() {}
};

class token_a : public base {
    public:
        token_a(int n) : n(n) {}
        virtual void print_token() { std::cout << n << std::endl; }

    private:
        int n;
};

class token_b : public base {
    public:
        token_b(std::string s) : s(s) {}
        virtual void print_token() { std::cout << s << std::endl; }

    private:
        std::string s;
};

int main(void) {
    std::stack<std::shared_ptr<base> > my_stack;
    my_stack.push(std::shared_ptr<base>(new token_a(5)));
    my_stack.push(std::shared_ptr<base>(new token_b("a word")));

    for ( int i = 0; i < 2; ++i ) {
        std::shared_ptr<base> pb = my_stack.top();
        pb->print_token();
        my_stack.pop();
    }

    return 0;
}

outputs: 输出:

paul@local:~/src/cpp/scratch$ ./stack
a word
5
paul@local:~/src/cpp/scratch$

The way I have solved this problem (in C, for a lisp interpretr, about 25 years ago, but same idea applies today) is to have a struct with a type and a union inside it: 我解决此问题的方式(大约25年前用C语言编写的Lisp解释器,但今天仍适用相同的想法)是在struct包含类型和union

struct Data   // or class
{
    enum kind { floatkind, intkind, stringkind, refkind };
    Kind kind;
    union
    {
       double f;
       int i;
       std::string s;
       Data* r;     // reference, can't use Data &r without heavy trickery. 
    } u;

    Data(double d) { kind = floatkind; u.f = d; }
    Data(int i) { kind = intkind; u.i = i; }
    ... 
}

std::stack<Data> st;

st.push(Data(42));
st.push(Data(3.14));

Just a guess, but the jvm probably treats everything as an object, so the stack is simply a collection of objects. 只是一个猜测,但是jvm可能将所有内容都视为一个对象,因此堆栈只是对象的集合。

You can do the same, if you create a base data object class and derive all your supported data types from it. 如果创建基础数据对象类并从中派生所有受支持的数据类型,则可以执行相同的操作。

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

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