RTTI and Pointer problem

Hello!
Im having a problem with the following code structure. The code compiles without problems but when i start the program i get an "exception: __non_rtti_object at memory location...". In the constructor of the model-class i still can get the object information of the node-class through the base pointer. Once the construction of the model-object is completed the class information somehow got lost. I would be very grateful if somebody could point the problem out. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef PATH_H
#define PATH_H

template<class T>
class Path
{
private:
    T* lastPathComponent;

	Path(const Path<T>&);
	Path<T>& operator=(const Path<T>& rv);

public:
	Path(const T& singlePath) { lastPathComponent = &singlePath; }
	~Path(void) {}

	T& getLastPathComponent(void) const
	{
		return *lastPathComponent;
	}
};

#endif //PATH_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef BASENODE_H
#define BASENODE_H

class BaseNode
{
private:
	BaseNode(const BaseNode&);
	BaseNode& operator=(const BaseNode& rv);

public:
	BaseNode(void) {};
	virtual ~BaseNode(void) {};
};

#endif //BASENODE_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef NODE_H
#define NODE_H

#include <string>
#include "BaseNode.h"
#include "Path.h"

class Node : public BaseNode
{
private:
	std::string name;

	Node(const Node&);
	Node& operator=(const Node& rv);

public:
	Node(void) { name = ""; }
	~Node(void) {}

	std::string getName(void) const { return name; }
	void setName(const std::string& name) { this->name = name; }
};

#endif //NODE_H 


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
#ifndef MODEL_H
#define MODEL_H

#include "Path.h"
#include "BaseNode.h"

class Model
{
private:
	TreePath<BaseNode* const>* dataNodePath;

	Model(const Model&);
	Model& operator=(const Model& rv);

public:
	Model(BaseNode* node)
	{
		dataNodePath = new TreePath<BaseNode* const>(node);

		std::cout << typeid(*getDataNodePath().getLastPathComponent()).name() << std::endl; // valid information
	}

	virtual ~Model(void)
	{
		delete dataNodePath; dataNodePath = NULL;
	}

	const TreePath<BaseNode* const>& getDataNodePath(void) const
	{
		return *dataNodePath;
	}
};

#endif //MODEL_H 


1
2
3
4
5
6
7
8
9
10
11
int main( )
{
	Node* x = new Node();
	x->setName("this is a node");
	Model* m = new Model(x);
	
	cout << typeid(*m->getDataNodePath().getLastPathComponent()).name() << endl; // exception: __non_rtti_object at memory location...
	
	delete m;
	delete x;
}
the fault is all in template class Path.
when you write dataNodePath = new Path<BaseNode * const>(node); in "Model.h",
you are actually initiating dataNodePath with &node.However, node is a temporary argument.
@whjatjx
Tank you very much - that solved the problem. i modified the template-class accordingly.
Topic archived. No new replies allowed.