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!

#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
#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
#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
#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
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;
}

Recommended Answers

All 3 Replies

I wonder what you are expecting to see? I haven't had much need to use the typeid operator to date so I have had fun fiddling around with this.

The exception you see indicates that you are attempting to use typeid on an expression that equates to an invalid object. I think you need to carefully examine the return types of your member functions.

!st template class will compile fine until the code is used. TreePath is not defined in the code you have shown

Path has some risky behaviour

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

using a pointer that you do not own without any error checking is dangerous
and are you sure you want a reference why not just return the pointer

cout << typeid(*m->getDataNodePath().getLastPathComponent()).name() << endl;

this line is doing too much at once there are many ways in which it could be failing
split it into variables and check each one in turn. there are several points where you could be failing in this line
It should not be in a constructor either

thanks for your replies!
I got it figured out now - the problem was with the template class...
In the constructor of model.h i tried to initialize the path-template with an temporary argument which caused the type-information to be lost.

@mattjbond

I wonder what you are expecting to see?

i only used the typeid operator to show my problem and to check which type of objects im operating on

@tetron

TreePath is not defined in the code you have shown

sorry that was a typo...

Path has some risky behaviour

thanks for the advice i modified the template class accordingly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.