Hello, I am working on developing an interpreted programming language for a capstone project and have hit a snag dealing with two classes. In the interpreter, the classes represent a scope containing a procedure pointer and the procedure pointing back to the scope containing it. The former to allow the call_procedure ( keytype N )
function to look up procedures and to avoid name conflicts (storing procedure pointers in the scope structure as opposed to a long std::map<keytype, procedure>
means multiple procedures can have the same name). The latter allows the procedure to access variables stored in a the scope in which it operates. keytype
is a template argument taken by the scope class, which determines what kind of value everything is mapped to; it is presently a std::vector<char>
.
As it is, this creates an infinite recursion of types in theory, but in practice just causes the compiler to spit an error at me.
The following testbed code demonstrates the behaviors I would like to have:
class a
{
public:
// links to an object of class B
void link (b * other, bool check)
{
test = other;
if ( !check ) { test -> link (this, 1); }
}
// example function
void say () { std::cout << "I am an A."; }
// example message-passing function
void say_other () { test -> say(); }
private:
b * test;
};
// acts just like class A, but with different pointers
class b
{
public:
void link (a * other, bool check)
{
test = other;
test -> link (this, 1);
}
void say () { std::cout << "I am a B."; }
void say_other () { test -> say(); }
private:
a * test;
};
int main ()
{
// create one of each object
a alpha;
b beta;
// give beta to a reference to alpha
beta.link(&alpha, 0);
// test functions from beta side
beta.say();
std::cout << std::endl;
beta.say_other();
std::cout << std::endl << std::endl;
// test functions from alpha side
alpha.say();
std::cout << std::endl;
alpha.say_other();
}
I am completely aware that the above code does not work; is there some way for me to have two separately-declared and otherwise independent classes contain pointers to each other?
(If I left out any explanation or if more code is needed, I can dredge through my scope and procedure source, but this is really more general than that specific application.)