Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
25 replies to this topic

#21 EWClay   Members   -  Reputation: 659

Like
1Likes
Like

Posted 29 December 2012 - 07:25 AM

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


Test = vEntity[0]->ComponentList["TransformComponent"]->getTypeName();


or
 

Test = vEntity[0]->ComponentList.begin()->second->getTypeName();

 

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.



Sponsor:

#22 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 29 December 2012 - 05:21 PM

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



#23 Servant of the Lord   Crossbones+   -  Reputation: 18203

Like
0Likes
Like

Posted 29 December 2012 - 05:55 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#24 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 29 December 2012 - 07:10 PM

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




}


#25 Álvaro   Crossbones+   -  Reputation: 12494

Like
0Likes
Like

Posted 29 December 2012 - 08:34 PM

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.

#26 Servant of the Lord   Crossbones+   -  Reputation: 18203

Like
0Likes
Like

Posted 29 December 2012 - 08:46 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS