Frage

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);

War es hilfreich?

Lösung

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!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top