Inheritence, Constructors, and Arrays oh my!

Started by
24 comments, last by psiko_scweek 17 years, 6 months ago
Quote:Original post by GPX
From my point of view, an object should never be in an "unitialized state" after construction. That's not the purpose of the constructor, its purpose is to construct the object, and bring it to an initialized state. Adding a default constructor "just for things to work", isn't reasonable.


"Uninitialized" is probably a misnomer here. A constructor should place an object in a known, consistent state which will not cause your destructor to explode, but it doesn't mean the object has to be immediately useful. The default constructor for a texture object may leave the texture "empty", requiring you to later call an initialization function to load an actual texture.

Not being able to fully initialize an object within the constructor is a commmon enough occurence. Sometimes you don't have a choice but to require a call to a post-construction initialization function.

Here, having a default constructor that creates an object using an ugly "default sprite" can be very useful.

Quote:Original post by deffer
If I get you right, you're saying that "if std::vector is using it, anybody can use it".


This is not how I read what he said. I believe he meant that since std::vector does the in-place initialization for you, there is no need to worry about whether placement new is an advanced topic or not.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Quote:Original post by deffer
Quote:Original post by GPX
Besides, it shouldn't be an "advanced topic" if you use an implementation such as std::vector.


If I get you right, you're saying that "if std::vector is using it, anybody can use it".
IMO, implementing (and doing it right) something as complex as std::vector is an advanced topic.


No, I meant using std::vector and not worrying about the implementation :-)
Although it is not complex at all, it's just a feature many people are not aware of, since it's not -that- useful. All it takes is using the syntax: MyClass* myObject = new(memoryPtr) MyClass(1,2,3), and later explicitly destructing the object using myObject->~MyClass().
Yoni Levy.
Quote:Original post by GPX
No, I meant using std::vector and not worrying about the implementation :-)


Then I'm sorry. [smile]

Quote:
Although it is not complex at all, it's just a feature many people are not aware of, since it's not -that- useful. All it takes is using the syntax: MyClass* myObject = new(memoryPtr) MyClass(1,2,3), and later explicitly destructing the object using myObject->~MyClass().


The destructor part is what I'm woried about. In case of non-automatic destruction things can get messy (exception catching and rethrowing, that stuff). Using in-place new you're essentially duplicating std::vector's code.
Damn, if only std::vector had parameter passing for its in-place construction - we're stuck with its via-copy push_back :(
Quote:Original post by Fruny
"Uninitialized" is probably a misnomer here. A constructor should place an object in a known, consistent state which will not cause your destructor to explode, but it doesn't mean the object has to be immediately useful. The default constructor for a texture object may leave the texture "empty", requiring you to later call an initialization function to load an actual texture.

Not being able to fully initialize an object within the constructor is a commmon enough occurence. Sometimes you don't have a choice but to require a call to a post-construction initialization function.

Here, having a default constructor that creates an object using an ugly "default sprite" can be very useful.


I agree you sometimes have to compromise design-wise (we both agree "full construction" is cleaner), guess I'm just more fanatic.

Quote:Original post by Fruny
This is not how I read what he said. I believe he meant that since std::vector does the in-place initialization for you, there is no need to worry about whether placement new is an advanced topic or not.


Yup, that's it :)
Yoni Levy.
Quote:
The destructor part is what I'm woried about. In case of non-automatic destruction things can get messy (exception catching and rethrowing, that stuff). Using in-place new you're essentially duplicating std::vector's code.
Damn, if only std::vector had parameter passing for its in-place construction - we're stuck with its via-copy push_back :(


But the whole point is being able to determine the values for the construction only later on, otherwise we could simply use a default constructor. Or perhaps now I am the one not understanding you :)
Yoni Levy.
Quote:Original post by GPX
Quote:
The destructor part is what I'm woried about. In case of non-automatic destruction things can get messy (exception catching and rethrowing, that stuff). Using in-place new you're essentially duplicating std::vector's code.
Damn, if only std::vector had parameter passing for its in-place construction - we're stuck with its via-copy push_back :(


But the whole point is being able to determine the values for the construction only later on, otherwise we could simply use a default constructor. Or perhaps now I am the one not understanding you :)


I guess so, 'cause I don't understand what your comment has to do with what you quoted, lol [grin]

What I meant, is that the in-place contruction part is quite straightforward. The destruction code, on the other hand, is harder. You have to exactly know where to put it. In addition, you'd have to compensate for any possible exceptions being thrown. And, in case of OP's array of objects, keeping track of how many objects are initialized thus far.

In the end, the exact code for that is in std::vector's code, which is why I said "you're essentially duplicating std::vector's code" - without parametrised construction (unfortunately, that's the limitation of the language itself).
Ok that STD::Vector stuff sounds like it would work exactly like I need it to,

but im having a problem getting it set up properly in my game...

any tips or information?

thanks again, you all have been extremely helpful!!
Quote:Original post by psiko_scweek
Ok that STD::Vector stuff sounds like it would work exactly like I need it to,


It's std::vector. C++ is case-sensitive.

Quote:but im having a problem getting it set up properly in my game...

any tips or information?


Not if you don't explain what the "problem" you're having is. We cannot read your mind, you know.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
hehehe,

sorry you guys are just so great! :-)

anyways, here is the added code:

[SOURCE]std::vector<PlayerBlocks> VerticalBlue;for(int i = 0, i < NumberOfBlocks, ++i){VerticalBlue.push_back(new PlayerBlocks(0,0,NULL));}[/SOURCE]


and the error im getting now is...

expected unqualified-id before 'for'

so, yeah im thinking i messed up somewhere... lol
Quote:Original post by Fruny
Quote:Original post by GPX
From my point of view, an object should never be in an "unitialized state" after construction. That's not the purpose of the constructor, its purpose is to construct the object, and bring it to an initialized state. Adding a default constructor "just for things to work", isn't reasonable.


"Uninitialized" is probably a misnomer here.


Hence my careful quoting :). I'm taking uninitialized here to mean "not initialized to a 'useful' state" rather than "... to a known/consistant/not undefined by the C++ standard/placeholder state". The quoting comes from this being a different definition for this bit of terminology than the C++ standard uses throughout it's specification.

And no, using std::vector (at least in it's basic capacities) isn't an advanced topic. But then you're using std::vector, not placement new.

Quote:Original post by psiko_scweek
anyways, here is the added code:

*** Source Snippet Removed ***


Problem #1: You try to push_back a pointer into a container of values ("hint": get rid of the new)

Problem #2:
Right: for ( initialization ; conditional ; iteration )Wrong: for ( initialization , conditional , iteration )
Problem #3?: Based on your error, you may have frogoten to #include <vector>, whatever defines PlayerBlocks, whatever defines NumberOfBlocks, or made a typeo with respect to any of these.

This topic is closed to new replies.

Advertisement