Strange problem template method

Hi!
I have a Cube and Plane class that inherit from Actor class. These classes have a mesh variable that I give a value calling the Content.Load method. The Plane class load perfectly, but the Cube class, when loaded, it "copy" his mesh data to plane mesh. It is very strange: before load Cube, Plane mesh have the correctly values; before return the cube mesh it having the correct values. But when it exit from Content.Load, plane mesh have the cube mesh data.

Can anybody help me and explain me this problem?

The code:

Cube.cpp

1
2
3
4
5
6
7
void Cube::Load(void)
{
    std::cout<<"BEFORE LOAD OBJ IN CUBE PLANE IS"<<getParent()->getEngine()->pln->mesh->meshparts[0]->vtn.size()<<std::endl;
    mesh = getParent()->getEngine()->content.Load<Mesh>("Models/untitled.obj");  // Have the correct value
    std::cout<<"AFTER LOAD OBJ IN CUBE PLANE IS"<<getParent()->getEngine()->pln->mesh->meshparts[0]->vtn.size()<<std::endl;  // Have the cube mesh data
    ApplyMaterials();
}


Content.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef GECONTENTMANAGER_H
#define GECONTENTMANAGER_H

#include <string>
#include <typeinfo>
#include <map>
#include <sstream>
#include <iostream>
#include "object.hpp"

#include "Plane.hpp"
#include "Mesh.hpp"

class Mesh;
class Shader;
class Texture;
class GEContentManager
{
    public:
        GEContentManager();
        Plane* pl;
        std::string rootDirectory;

        template <class T> T* Load(std::string filename)
        {
            if(typeid(T) != typeid(Mesh) && typeid(T) != typeid(Shader) && typeid(T) != typeid(Texture))
            {
                std::cerr<<"ERROR: Only you can load a mesh, shader o texture data"<<std::endl;
                //exit(1);
            }


            std::map<std::string, object*>::iterator it = loadedObjects.find(filename);


            if(it!=loadedObjects.end())
            {
                std::cout<<"El objeto "<<it->first<<" ya habia sido cargado. Devolviendo"<<std::endl;
                return (T*)it->second;
            }



            std::stringstream sstm;
            sstm << rootDirectory << filename;
            std::cout<<"Load file "<<sstm.str()<<std::endl;
            std::string path = sstm.str();

            T* t = new T;

            t->Load(path);

            loadedObjects.insert(std::pair<std::string, object*>(filename,t));
            if(filename != "Models/plane.obj")
            {
                std::cout<<" ----------------PLANE IS "<<pl->mesh->meshparts[0]->vtn.size()<<std::endl;  // Plane have the correct data yet
            }

            return t;
        }

    protected:

    private:

        std::map<std::string, object*> loadedObjects;
};
Last edited on
Consider using a debugger, watch when the pointer (or what it points to) changes.
http://www.delorie.com/gnu/docs/gdb/gdb_30.html

Again, try to create a minimal setup that does reproduce your issue.
If you want, upload it to github.
I am using Codeblocks with watches window, but i can´t extends the mesh object from plane class:

http://imageshack.us/photo/my-images/855/sinttulolph.png/
¿static?

http://cplusplus.com/doc/tutorial/classes2/
Static data members of a class are also known as "class variables", because there is only one unique value for all the objects of that same class. Their content is not different from one object of this class to another.
Thanks!! I was the problem!!
Topic archived. No new replies allowed.