S
Steven T. Hatton
Below is some code I wrote to get a better understanding of the dynamic
verses static type resolution. My intention was to see if there was a way
to use references for the static access to the subobjects of an object of
derived type. I thought I had seen a way to use references to statically
access member functions in a way syntactically similar to how pointers
dynamically access the same objects member functions.
The overall objective is to 'navigate' the diamond in the most elegant and
safe way. I had hoped to use this as a follow up to the discussion on
references and pointers and other objects(sic). Unfortunately I wasn't able
to find a way to do what I thought I could.
Do othere people have comments on how best to 'navigate' the class
hierarchy?
#include <string>
#include<iostream>
using std::string;
class North
{
public:
North(const string& name="North"):m_name(name)
{}
virtual string& toString() { return m_name; }
private:
string m_name;
};
class East : virtual public North
{
public:
East(const string& name="East"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};
class West : virtual public North
{
public:
West(const string& name="West"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};
class South : public East, public West
{
public:
South(const string& name="South"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(East::toString());
*ret += "::";
*ret += West::toString();
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};
int main()
{
using std::cout;
using std::endl;
North n;
East e;
West w;
South s;
cout<<n.toString()<<endl;
cout<<e.toString()<<endl;
cout<<w.toString()<<endl;
cout<<s.toString()<<endl;
North ns = static_cast<North>(s);
East es = static_cast<East>(s);
West ws = static_cast<West>(s);
cout<<ns.toString()<<endl;
cout<<es.toString()<<endl;
cout<<ws.toString()<<endl;
}
/*OUTPUT*/
North
North::East
North::West
North::East::North::West::South
North
North::East
North::West
verses static type resolution. My intention was to see if there was a way
to use references for the static access to the subobjects of an object of
derived type. I thought I had seen a way to use references to statically
access member functions in a way syntactically similar to how pointers
dynamically access the same objects member functions.
The overall objective is to 'navigate' the diamond in the most elegant and
safe way. I had hoped to use this as a follow up to the discussion on
references and pointers and other objects(sic). Unfortunately I wasn't able
to find a way to do what I thought I could.
Do othere people have comments on how best to 'navigate' the class
hierarchy?
#include <string>
#include<iostream>
using std::string;
class North
{
public:
North(const string& name="North"):m_name(name)
{}
virtual string& toString() { return m_name; }
private:
string m_name;
};
class East : virtual public North
{
public:
East(const string& name="East"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};
class West : virtual public North
{
public:
West(const string& name="West"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};
class South : public East, public West
{
public:
South(const string& name="South"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(East::toString());
*ret += "::";
*ret += West::toString();
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};
int main()
{
using std::cout;
using std::endl;
North n;
East e;
West w;
South s;
cout<<n.toString()<<endl;
cout<<e.toString()<<endl;
cout<<w.toString()<<endl;
cout<<s.toString()<<endl;
North ns = static_cast<North>(s);
East es = static_cast<East>(s);
West ws = static_cast<West>(s);
cout<<ns.toString()<<endl;
cout<<es.toString()<<endl;
cout<<ws.toString()<<endl;
}
/*OUTPUT*/
North
North::East
North::West
North::East::North::West::South
North
North::East
North::West