fork download
  1. #include <stdlib.h>
  2. #include <malloc.h>
  3. #include <memory.h>
  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7.  
  8. class Node;
  9. class CustomObject;
  10.  
  11. class CustomObject
  12. {
  13. public:
  14. std::string name;
  15. Node* parent;
  16. CustomObject(std::string n_name);
  17. std::string getName();
  18. void setParent(Node* pparent);
  19. Node* getParent();
  20. };
  21.  
  22. CustomObject::CustomObject(std::string n_name)
  23. {
  24. name = n_name;
  25. }
  26.  
  27. std::string CustomObject::getName()
  28. {
  29. return name;
  30. }
  31.  
  32. void CustomObject::setParent(Node* pparent)
  33. {
  34. parent = pparent;
  35. }
  36.  
  37. Node* CustomObject::getParent()
  38. {
  39. return parent;
  40. }
  41.  
  42. class Node
  43. {
  44. public:
  45. std::string name;
  46. Node* parent;
  47. std::vector<Node> childs;
  48. Node(std::string n_name);
  49. void attach(Node* nd);
  50. void attach(CustomObject* anm);
  51. std::string getName();
  52. void setParent(Node* pparent);
  53. Node* getParent();
  54. std::vector<CustomObject> mCustomObjects;
  55. };
  56.  
  57. Node::Node(std::string n_name)
  58. {
  59. name = n_name;
  60. }
  61.  
  62. std::string Node::getName()
  63. {
  64. return name;
  65. }
  66.  
  67. void Node::setParent(Node* pparent)
  68. {
  69. parent = pparent;
  70. }
  71.  
  72. Node* Node::getParent()
  73. {
  74. return parent;
  75. }
  76.  
  77. void Node::attach(Node* nd)
  78. {
  79. childs.push_back(*nd);
  80. nd->setParent(this);
  81. }
  82.  
  83. void Node::attach(CustomObject* anm)
  84. {
  85. mCustomObjects.push_back(*anm);
  86. anm->setParent(this);
  87. }
  88.  
  89. class Game
  90. {
  91. std::string name;
  92. public:
  93.  
  94. std::vector<Node> nodes;
  95. std::vector<CustomObject> mCustomObjects;
  96. Game(std::string nm);
  97. };
  98.  
  99. Game::Game(std::string nm)
  100. {
  101. name = nm;
  102. }
  103.  
  104. void show_message(CustomObject* anm)
  105. {
  106. std::cout << anm->getName().c_str() << "\n";
  107. std::cout << anm->getParent()->getName().c_str() << "\n";
  108. }
  109.  
  110. int main()
  111. {
  112. Game* mGame = new Game("MyGame");
  113.  
  114. Node* mNode = new Node("MyNode");
  115. mGame->nodes.push_back(*mNode);
  116. CustomObject* mCustomObject = new CustomObject("Object1");
  117. mGame->mCustomObjects.push_back(*mCustomObject);
  118. mNode->attach(mCustomObject);
  119.  
  120. show_message(mCustomObject);
  121.  
  122. for (std::vector<CustomObject>::iterator itr = mGame->mCustomObjects.begin(); itr != mGame->mCustomObjects.end(); ++itr)
  123. {
  124. CustomObject* cObj;
  125. cObj = &(*itr);
  126. show_message(cObj);
  127. }
  128. }
Runtime error #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Standard output is empty