|
I don't think this kind of thing can be done unless you've got some brand spanking new C++0x[^] compliant compiler. Specifically the C++0x "Initializer lists" feature.
Steve
|
|
|
|
|
Guess I am waiting for Visual Studio 2010 SP1
http://www.contract-developer.tk
|
|
|
|
|
|
I have looked the std::array and I will see what I can do about casting that to my vector template.
#include <array>
http://www.contract-developer.tk
|
|
|
|
|
That was another waste of time.
Sure seems VC 2010 is hostile to data processing.
http://www.contract-developer.tk
|
|
|
|
|
If you want to do something with an array and not a pointer to a lump of data then declare the function as:
template <std::size_t N> my_class &operator=(T (&t)[N])
where T is the type you're templating on. Usually what you do is have a copy constructor that takes the same parameters and then you implement operator= in terms of it (which can improve exception saftety as well if you do it right):
template <std::size_t N> my_class &operator=( T (&t)[N] )
{
my_class temp( t );
std::swap( temp. *this );
return *this;
}
The important thing is getting the size of the array into the function.
Another way of doing this, which is common in the STL, is to specify a range of interators, in this case you could use a range of T pointers:
my_class &operator=( const T *begin, const T * const end )
I'd strongly recommend you grab a copy of "Exceptional C++" by Herb Sutter - it's got loads of guidance on how to write assignment operators.
Cheers,
Ash
|
|
|
|
|
a while ago I wrote a vector template to assist in a project, it works OK.
I was considering the effect inheriting the std::vector and make all of the functionality available and simply attach my new functionality on top of that.
Is this practical? Here is the vector as it stands today.
template <class base> class vector {<br />
public:<br />
std::vector<base> data;<br />
friend class matrix<base>;<br />
vector operator+(const vector &that) {
vector ret;<br />
assert(this->data.size() == that.data.size());<br />
ret.resize(this->data.size());<br />
#pragma omp parallel for<br />
for (int i = 0; i < this->data.size(); i++)<br />
ret.data[i] = this->data[i] + that.data[i];<br />
return ret;<br />
}<br />
vector operator-(vector that) {
vector ret;<br />
assert(this->data.size() == that.data.size());<br />
ret.resize(this->data.size());<br />
#pragma omp parallel for<br />
for (int i = 0; i < this->data.size(); i++)<br />
ret.data[i] = this->data[i] - that.data[i];<br />
return ret;<br />
}<br />
vector operator* (base &that) {
vector ret;<br />
ret.resize(this->data.size());<br />
#pragma omp parallel for<br />
for (int i = 0; i < this->data.size(); i++)<br />
ret.data[i] = this->data[i] * that;<br />
return ret;<br />
}<br />
double operator* (vector &that) {
double ret;<br />
#pragma omp parallel for<br />
for (int i = 0; i < this->data.size(); i++) <br />
ret += this->data[i] * that.data[i];
return ret;<br />
}<br />
vector operator/ (base &that) {
vector ret;<br />
ret.resize(this->data.size());<br />
#pragma omp parallel for<br />
for (int i = 0; i < this->data.size(); i++)<br />
ret.data[i] = this->data[i] / that;<br />
return ret;<br />
}<br />
vector operator= (vector &that) {
for (int i=0; i < that.data.size(); i++)<br />
this->data[i] = that.data[i];<br />
return *this;<br />
}<br />
base& operator[] (size_t index) { <br />
return this->data[index];<br />
}<br />
bool operator== (vector &that) { <br />
bool temp = true;<br />
for (int i=0; i < that.data.size(); i++)<br />
if (this->data[i] != that.data[i]) temp = false;<br />
return temp;<br />
}<br />
base norm(void) { <br />
base result = (base)0.0;<br />
#pragma omp parallel for<br />
for (int i = 0; i < this->data.size(); i++) {<br />
result += this->data[i] * this->data[i];
}<br />
return sqrt(result);<br />
}<br />
size_t size(void) {<br />
return data.size();<br />
}<br />
vector resize(size_t newsize) {<br />
data.resize(newsize);<br />
return *this;<br />
}<br />
vector clear(void) {<br />
data.clear();<br />
return *this;<br />
}<br />
};<br />
http://www.contract-developer.tk
|
|
|
|
|
The general concensus is don't inherit from an STL collection - they're not designed for it.
The two ways I'd recommend going about extending an STL collection is either bundle it up in another class (which you've done) OR just write free functions to do the extended functionality. From your example it looks like you want a geometric vector and not a collection of doodads that might or might not have some geometric relationship.
Incidentally if you want to continue using containment the way you have make the std::vector private. You really don't want a class exposing data through it's public interface, it's really hard to test and some idiot will maul it for you - even if that idiot turns out to be you several months down the line when you've forgotten what you did.
Cheers,
Ash
|
|
|
|
|
I guess I will need to add more accessors then to make it resemble the std::vector
My goal is to resemble the std::vector and bolt on some linear algebra
http://www.contract-developer.tk
|
|
|
|
|
|
I find that adding:
template <class base> class vector : std::vector<base>
does not seem to provide a great deal of change? So what impact if any does inheriting the object get me?
http://www.contract-developer.tk
|
|
|
|
|
VeganFanatic wrote: So what impact if any does inheriting the object get me?
again, STL containers do not provide virtual destructors[^]
|
|
|
|
|
Should I simply add more code and add formal constructors and destructors? Go for base* data construct instead?
http://www.contract-developer.tk
|
|
|
|
|
VeganFanatic wrote: Should I simply add more code and add formal constructors and destructors?
no matter what you do, because STL containers do not have virtual dtors, the destructor in your derived class will not be called.
http://www.devx.com/tips/Tip/13509[^]
|
|
|
|
|
This is only an issue if he deletes the object through the base class.
Steve
|
|
|
|
|
well, i provided as many links to detailed explanations as i could stand to look up.
|
|
|
|
|
Chris, those links are "things to be noted" not "the Bible".
The STL itself is designed by inheriting between classes that don't have virtual destructor. Think to iofstream versus istream and ostream
STL containers don't have virtual destructors because they are designed to be non-polymophic and to exist into a closed scope.
If you respect this, that means don't do things like (pseudocode)
vector* p = new myvector;
delete p;
but just
{
myvector v;
}
you are in any case in a right position.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
i thought it would be better to tell the OP where to find relevant information, rather than to try explaining the entire topic and trying to find out which bits he needed to specifically worry about.
|
|
|
|
|
Heey guys,
Little question, long answer:P
How to find a array of byte in vc with readprocessmemory?
I need to find: 01 00 00 00 00 73 74 65 72 2F
How to do this:P
|
|
|
|
|
I found someone who had the exact same question as yours over at Cheat Engine[^]. That guy got a reply he was satisfied with.
|
|
|
|
|
I don't know how to express.So,I with his words.But he needs to Delphi and I need to the vc
|
|
|
|
|
|
What have you tried so far? Do you have a starting address from which to read?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi I have using MFC dialog application. in that it is calling another dialog.
in 2nd dialog i am enumerating the windows using enumwindows api. after calling the enumwindows application is freezing or hanging. any idea about this.
|
|
|
|
|
Does your callback get called at all?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|