I tried to do a simple factory but ended up with a wierd error?

Started by
24 comments, last by Servant of the Lord 11 years, 3 months ago

The key for the map is a string. Unfortunately 0 will automatically convert to a string, so there is no compilation error.

You might want
[source]
Test = vEntity[0]->ComponentList["TransformComponent"]->getTypeName();
[/source]
or
[source]
Test = vEntity[0]->ComponentList.begin()->second->getTypeName();
[/source]
Interesting fact: BaseComponent should really have a virtual destructor but you get away without one in this case because the smart pointer is smart enough to call the right one anyway. Still, it's better to have one.

Advertisement

Why should I have a virtual destructor for my base component? sorry still new to coding in overall and pretty new to c++ :/

When a derived class is accessed through a base class pointer, and a virtual function overridden by the derived class is called, the derived class' version is the one that gets called.

Likewise, with a virtual destructor, when the base class' destructor is called, if it is virtual, then the derived constructor gets called.

When creating a derived class, there's no normal way you can't accidentally not call the derived class' constructor.

BaseClass *base = new DerivedClass;

In the above code, the DerivedClass is constructed by 'new' before it is pointed to by a BaseClass pointer, so the derived constructor gets call

However, imagine this:

BaseClass *base = new DerivedClass; delete base;

If BaseClass doesn't have a virtual destructor, DerivedClass' destructor will never get called. If you are supposed to deallocate memory or release some system resource in DerivedClass' constructor, that will fail to occur.

so "delete" calls upon destructor?

so, if I wanted to delete everything inside a class example

or do I just need to delete/release special stuffs inside it?

like buffers and such things? or do I need to delete normal stuffs to like int bool etc?

and do I need to have same parameters in the destructor as I had in the constructor? if its possible to have more than one destructor.

Just got this problem here.

my BaseComponent only have one constructor that take one parameter agruement, how come I cant overload it in my derivedclass?

cause my derived class says my BaseComponent have no default constructor

am guessing the default constructor is one that takes zero agruement in its parameter. is there a way going around it?

cause I do not want my program to be able to create BaseComponent with zero agruement in any possible way


class test
{
public:

~test()
{
   delete a;
   delete b;
   delete c;
   //Etc?


}
   int a;
   int b;
   int c;
//Etc




}
so "delete" calls upon destructor?
so, if I wanted to delete everything inside a class example
or do I just need to delete/release special stuffs inside it?
like buffers and such things? or do I need to delete normal stuffs to like int bool etc?
and do I need to have same parameters in the destructor as I had in the constructor? if its possible to have more than one destructor.

I'm not trying to be mean, but I don't think we can help you figure all these things out in a forum. You are seriously confused. I suggest you read a book and/or some tutorials. Test your understanding with little test programs. And post in "For Beginners" if you need help in the future.

Anything you manually 'new', you need to manually delete. If you didn't 'new' it, you don't need to 'delete' it.

Since you didn't new 'a','b', and 'c', you don't need to delete them either.

'a','b', and 'c' belong to the class, and since the class allocated them, the class will also delete them.

This topic is closed to new replies.

Advertisement