2

Alright, here I have this small example of my complex class

class LivingObject
{
 Ogre::SceneNode* myNode;
 Gorilla::ScreenRenderable* myScrRend;
 Gorilla::Layer* myRendLayer;
 Gorilla::Rectangle* myRendRect;
 int Health,Energy,Strength,Dexterity,Intelligence;
 float Speed,posX,posY,posZ;
 //Assortment of functions
};//Note: Specific members and functions are public/private, but is not relevant

Here is some game class info

class myGame
{
 Ogre::Viewport* myViewport;//random
 LivingObject LiveObjectArray[100]//question 1: holds the array of objects from a parsed file
 std::vector<std::tr1::shared_ptr<LivingObject> > spawnList;//question 2
};

1) How should I be declaring LivingObject where I can copy it later(the current method I am using gives an error: conversion from 'LivingObject*' to non-scalar type 'LivingObject' requested)LivingObject TestObj=new LivingObject;

1a) What do I do with pointers like LivingObject::myNode when making a new object, should I make them objects? or is something else wrong?(Note: I am using Ogre3D and this is the way that the tutorials made me set everything...)

2) When the above is solved, how would I then put it into a shared_ptr vector and access that specific element for functions(e.g. spawnList[15].(or ->)Attack(target);

Molma
  • 171
  • 9
  • `new` returns a pointer, so your definition should read `LivingObject* TestObj = new LivingObject;` – coproc Jan 16 '13 at 20:41
  • Alright, and would I need `new` to just make another object? Also what do I do with the error I am getting along with the Ogre3d recommended pointers? – Molma Jan 16 '13 at 20:53
  • 1
    If not then do I just do `LivingObject NewObj;NewObj.giveVars(...);`? – Molma Jan 16 '13 at 20:55

1 Answers1

2

1) In order to copy an object, use this code:

string s;
string t = s;

1a) What do those pointers represent? If they represent exclusive ownership, you must copy the objects they point to when copying the owning object. Check any good text's introduction to constructors and destructors. Consider making the LivingObject class non-copyable.

2) Try this:

shared_ptr<T> p(new T);
vector<T> v;
v.push_back(p);
...
shared_ptr<T> q = v[0];
q->member_function();

As a last advise, you need a good C++ book. Another great resource is an online community of other users like this one here. If possible, try to reduce your code though. It's enough that LivingObject has one example pointer. Good luck!

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • I greatly thank the information about the vectors. As for 1a) `Ogre::SceneNode* myNode` is an object variale That I have created in ogre3d: `Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("HeadNode");` This was taken right from the first tutorial of Ogre3D. How would this object affect `LivingObject` when its a pointer? Or can I just make it an object by `Ogre::SceneNode headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("HeadNode");`? OR would I just use it as-is in the object? – Molma Jan 17 '13 at 01:55
  • Also What would you consider a good text for important C++ info such as this? or maybe a few websites? – Molma Jan 17 '13 at 02:00
  • I used http://www.icce.rug.nl/documents/cplusplus/ when learning C++. That was ~13 years ago though and while the document was updated since then, I haven't looked at it too deeply. There is also a version of of Thinking in C++ for download. I also have Bjarne Stroustrup's C++ book here which is rather good and has a few examples or very elegant code. – Ulrich Eckhardt Jan 18 '13 at 17:48
  • Still wondering what to do with pointer objects such as `Ogre::SceneNode*` and `Gorilla::ScreenRenderable*` – Molma Jan 19 '13 at 02:45
  • Sorry for the late acceptance, I haven't been able to correctly apply this until I learned about a few other things first. Thanks for the information. – Molma Feb 28 '13 at 19:26