Reminder: Internet Explorer 6 or below are NOT supported.





There are currently 97 users playing Freelancer on
41 servers. | February. 26, 2021 |
The Starport Forum Index
>
The Starport
>
General Discussion
>
Spam Board
>
n00b's programming question
Browsing this Thread:
1 Anonymous Users
n00b's programming question |
||||
---|---|---|---|---|
Home away from home
![]() ![]() Joined:
2009/7/30 4:40 From P.R.C
Group:
Registered Users Senior Members
Posts:
849
![]() |
The story is I'm acutally making a FLHook plugin.
In the plugin, I have a group of class (Let's say, derived class) which shared same Base class. And a Manager class used to control and send event one by one to those derived classes with method call. Code like this: https://gist.github.com/raincious/6b786a9c7f9e4fa6c0b3 My question is: 1, Can it be done? Notice I each derived class can have different size and method, And the Base class may have unvirtual methods. 2, When I try to delete, will be memory which assigned to derived classes can be completely release (memory size is different to Base class)? I'm new to C++, so new .... that I can't know if I truly need a shared_ptr or not .... Please help, Thank you!
Posted on: 2015/1/18 5:32
|
|||
Sorry for my poor english... |
||||
|
Re: n00b's programming question |
||||
---|---|---|---|---|
Just can't stay away
![]() ![]() Joined:
2008/6/16 20:41 Group:
Registered Users FLServer Admins Trusted Speciality Developers
Posts:
312
![]() |
If your Base class has at least one virtual member function, make the base class destructor virtual. The result is undefined when you delete a pointer of a base class, pointing to one of the derived classes.
Also for iterators its better to have a pre increment rather than a post increment. And use initializer lists for classes rather than to assignment in the constructors body.
Posted on: 2015/1/18 9:20
|
|||
![]() |
||||
|
Re: n00b's programming question |
||||
---|---|---|---|---|
Home away from home
![]() ![]() Joined:
2009/7/30 4:40 From P.R.C
Group:
Registered Users Senior Members
Posts:
849
![]() |
Quote:
OK, now I have virtual destructor and pre increment iterator (I found this after readed your reply: http://stackoverflow.com/questions/13 ... een-iterator-and-iterator). So I still have Undefined Behaviour problems even after 'correct' delete? How to solve that problem? Or I can only go with smart pointer (The shared_ptr)?
Posted on: 2015/1/18 10:09
|
|||
Sorry for my poor english... |
||||
|
Re: n00b's programming question |
||||
---|---|---|---|---|
Just can't stay away
![]() ![]() Joined:
2008/6/16 20:41 Group:
Registered Users FLServer Admins Trusted Speciality Developers
Posts:
312
![]() |
you should not have, make sure you have a virtual destructor in all upper base classes available, so when you have derived1 and derived2 and derived inherits from derived1 then you also need a virtual destructor in derived1 and so on and so forth.
I am not so familiar with shared_prt but in principal you could use them too. But even shared_ptr wont solve the problem of eventual non-virtual destructors. It only takes over destructing if you forget to destruct your objects (as far as i know).
Posted on: 2015/1/18 10:44
|
|||
![]() |
||||
|
Re: n00b's programming question |
||||
---|---|---|---|---|
Home away from home
![]() ![]() Joined:
2009/7/30 4:40 From P.R.C
Group:
Registered Users Senior Members
Posts:
849
![]() |
OK, I will carefully go with raw pointer and "virtual destructor" every upper classes too.
Thank you Huor.
Posted on: 2015/1/18 12:01
|
|||
Sorry for my poor english... |
||||
|
Re: n00b's programming question |
||||
---|---|---|---|---|
Starport Admin
![]() ![]() Joined:
2009/2/21 21:42 Group:
Webmasters Registered Users
Posts:
3520
![]() |
Naked pointers is asking for trouble. Get a C++11 compliant compiler (VS2012 and up) and use unique_ptr.
Posted on: 2015/1/18 15:49
|
|||
"Cynicism is not realistic and tough. It's unrealistic and kind of cowardly because it means you don't have to try." -Peggy Noonan |
||||
|
Re: n00b's programming question |
||||
---|---|---|---|---|
Home away from home
![]() ![]() Joined:
2009/7/30 4:40 From P.R.C
Group:
Registered Users Senior Members
Posts:
849
![]() |
Quote:
Wow, I learned a lot today LOL. Yeah, I think unique_ptr is better since I don't need copy them. And looks I also don't need to care about class slicing because memory will be erased by internal magic stuffs itself. (Is I'm right?) OK, I will change code and go with smart pointer. Thank you FF.
Posted on: 2015/1/18 16:37
|
|||
Sorry for my poor english... |
||||
|
Re: n00b's programming question |
||||
---|---|---|---|---|
Starport Admin
![]() ![]() Joined:
2009/2/21 21:42 Group:
Webmasters Registered Users
Posts:
3520
![]() |
Yeah, if you use a unique_ptr the memory will be released and the destructors called as soon as the pointer gets out of scope. This will be the case even if there's an exception somewhere, whereas an explicit delete call might be skipped due to an exception.
Posted on: 2015/1/18 18:28
|
|||
"Cynicism is not realistic and tough. It's unrealistic and kind of cowardly because it means you don't have to try." -Peggy Noonan |
||||
|