calling destructor versus new object

Started by
45 comments, last by Sigvatr 10 years, 7 months ago

That wasn't so bad, having done the right preparations smile.png

I've implemented it now for my engine::scene class (IO), all working fine, and no problems anymore with the default assignment/ copy operator. Even following the rule of three, DIY destructor not needed anymore.

A few conclusions/ thaughts:

- I assume there's nothing to do with a vector in my constructor (not setting to NULL or so)

- vector.reserve function is useless when I simply use vector.resize, because in my/ this case I know the exact number/ size

(if I append an object later on, I'll just use 'push back' and it will reserve space for me anyway)

- no forward declarations possible anymore in my header files of the class, because the vector types are no longer pointers

(more includes needed in header file)

- for loops get less readable when using vector.size, returns a vector<int>::size_type instead of regular int

example: for(vector<int>::size_type mi=0;mi<mMeshInstances.size();++mi)

I might consider to use iterators later on, but for now this works fine (no signed/unsigned warnings)

Struggle; when going through my scene file the 1st time and count the needed sizes for lights, meshes, etc etc., I have to save these numbers. So basically I'm using both my member variabel int's to find number of meshes, lights etc.. But afterwards I can use vector.size() to go through all objects. Not sure how I can eliminate having the int's as member vars having the same value as the vector's.size(). Unless I change my allocate memory function with a huge number of input parameters).

Any input is appreciated as always.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement
For your last point, you can use the 'auto' keyword for the loop counter type instead of explicitly typing it out.
eg. auto mi=0;....

o3o

- I assume there's nothing to do with a vector in my constructor (not setting to NULL or so)
- vector.reserve function is useless when I simply use vector.resize, because in my/ this case I know the exact number/ size
(if I append an object later on, I'll just use 'push back' and it will reserve space for me anyway)
- no forward declarations possible anymore in my header files of the class, because the vector types are no longer pointers
(more includes needed in header file)

More or less correct.

- for loops get less readable when using vector.size, returns a vector<int>::size_type instead of regular int
example: for(vector<int>::size_type mi=0;mi<mMeshInstances.size();++mi)
I might consider to use iterators later on, but for now this works fine (no signed/unsigned warnings)

Well, see Waterlimon's response any my comment below...

Struggle; when going through my scene file the 1st time and count the needed sizes for lights, meshes, etc etc., I have to save these numbers. So basically I'm using both my member variabel int's to find number of meshes, lights etc.. But afterwards I can use vector.size() to go through all objects. Not sure how I can eliminate having the int's as member vars having the same value as the vector's.size(). Unless I change my allocate memory function with a huge number of input parameters).

Any input is appreciated as always.

Sounds like those int variables don't have to be members. If you only use them locally to count, then make them local variables.

For your last point, you can use the 'auto' keyword for the loop counter type instead of explicitly typing it out.
eg. auto mi=0;....

If you're using the auto keywords, you can use ranged for loops as well.


for(auto &element : mMeshInstance) {
}

Thanks, I'll definately go for the 'auto' keyword.

Regarding the counters, I see 2 options when I keep them local scope:

1. pass all of them through to as function parameters to my AllocateMemory member function (which does the resizes)

2. do the 'resizes' within the function where I go through the file

(3. create a simple struct to save all counters and pass the struct through to the allocatememory function)

I like the idea of having separate functions for separate clear goals, like 'AllocateMemory'.

Not such a biggy, I'll choose one of the options and go on creating nice new stuff

Ps; just finished all changes for the d3dscene class also, worked out quite well.

With one minor bump, I had to change a header in the 'vector' (header) file, because aligned input was not alowed in the 'resize' function:


	void resize(size_type _Newsize, const _Ty &_Val)
//	void resize(size_type _Newsize, _Ty _Val)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Note that the 'auto' keyword requires a C++ 11 compiler. That's generally not a problem on the PC with a modern compiler, but can be on more obscure platforms, or with older compilers.

However, I've not yet come across a platform where std::vector<whatever>::size_type isn't equivalent to size_t, and this suggests that the standard says it will always be that way.

Changing the <vector> header is a very bad plan. It's a system header shared by all projects, and is not designed to be modified at all. If you modify it your code won't work correctly for anyone else. There's almost certainly a correct way to achieve what you want to do, but I'm not sure I understand the problem you're trying to work around by modifying it.

Hi adam.
I get a compiler error when I try to use a std::vector for my class that contains 'aligned data', in this case a D3DXMATRIXA16.
I looked it up and it seems to be widely known problem, that you can't pass const data to the std::vector resize function, and the header change was one of the solutions. Others were using another library for 'vectors', like stlport or other 3rd party libraries. I preferred the header change. It was also confirmed that in msvc2012 the issue isn't there anymore (and the header should be corrected)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

i am always amazed at how ppl manage to make very simple things incredibly messy.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

What of course is not my intention. What would be the better/ easier solution on this?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

What of course is not my intention. What would be the better/ easier solution on this?

anything that doesn't involve hacking into the standard library making it impossible for anybody else to compile your stuff? Or calling constructors and destructors manually?

PPL write graphics engines with C++ all around the world without need to brake the language this way.. it's just the wrong approach to things. I would never want somebody with that attitude in my team.

Just fix your code and get on with it.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

Maybe my question was unclear. I'd really like to solve it without changing the header of the vector resize function. But I don't know how, other then using an other (3rd) party library for vectors, which I'm trying to prevent. Besides this I'm curious why ms changed it in msvc12, maybe this is done because of newer insights. I really dont know.

Do you have any ideas how I could solve it, using the std library vector?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement