myclass myclass::operator+(const myclass& array2) const
{
myclass newArray(array, size);
return newArray;
}
当我尝试返回array2时,它可以工作。 但是newArray,它终止了程序。为什么?
我对OOP的工作不多。因此,我不知道我应该在这里给予足够的东西。请详细说明一下。
程序有赋值运算符但在这里我不使用它:
int main(){
int test[35]={0 to 35} //filled using for loop
myclass a1(test, 10); // constructor works for filling a1 by first 10 element of test
myclass a2(test, 20);
a1+a2;
}
答案 0 :(得分:1)
如果没有最小的,完整的,可验证的例子,就不可能分辨出什么是错的。在这种情况下,这意味着您的类定义的相关部分和main()
函数。
如果这有帮助,以下是编写高效operator+
和operator-
的三种不同方法的示例。他们使用一些相当先进的技术。 operator+
的实现依赖于编译器使用提供的移动构造函数并优化掉任何冗余副本。 operator-
的实现调用程序构造函数,该构造函数使用一元版本的伪参数,然后是二进制版本的函数对象。
#include <array>
#include <cassert>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <new>
#include <utility>
#include <vector>
using std::cout;
using std::endl;
using std::size_t;
// A four-element vector, implemented internally with std::vector.
class vec4i {
private:
static constexpr size_t size = 4U;
/* Object holding the vector data. The compiler knows how to destroy it,
* so we do not need to write our own destructor.
*/
std::vector<int> data;
/* Empty class used as a dummy parameter to tell the compiler that we are
* calling the programmatic constructor, not the copy constructor.
*/
struct op_opposite {};
// Programmatic constructor used by unary operator-:
vec4i( const vec4i& v, const op_opposite& /* unused */ )
: vec4i()
{
for (size_t i = 0; i < size; ++i)
data[i] = -v[i];
}
/* The type of a generic function object implementing a binary function. */
using binary_fun = std::function<int(const int, const int)>;
/* We're going to demonstrate a different approach for operator-, a
* programmatic constructor using an arbitrary binary function object from
* <functional>.
*/
vec4i( const vec4i& u, const vec4i& v, const binary_fun f )
: vec4i()
{
for ( size_t i = 0; i < size; ++i )
data[i] = f(u[i], v[i]);
}
public:
// Default constructor initializes matrix to zeroes:
vec4i() : data(size) {}
// Constructors that provide values:
vec4i( const std::vector<int>& v ) : data(v) {}
vec4i( const int x, const int y, const int z, const int w )
: data{x,y,z,w}
{}
vec4i( const int v[size] )
: data(size)
{
for ( size_t i = 0; i < size; ++i )
data[i] = v[i];
}
// Copy constructor that makes a deep copy:
vec4i(const vec4i& v) : data(v.data) {}
// Move constructor that avoids a deep copy:
vec4i(vec4i&& v) : data(std::move(v.data))
{}
// One final example, using STL iterators, since it's a one-liner:
vec4i(const std::array<int, size>& a) : data(a.begin(), a.end()) {}
/* Provide an assignment operator from any object that can be used to
* construct this type. Ensure that the class does not assign from its own
* internal data.
*/
template<class T> vec4i& operator=(const T& v)
{
// Check for self-assignment.
if ((void*)this != (void*)&v) {
// Call the destructor, then create a new object in-place.
this->~vec4i();
new(this) vec4i(v);
}
return *this;
}
vec4i operator=(vec4i&& v)
// A more efficient assignment that moves rather than copies the data:
{
if ((void*)this != (void*)&v) {
data.clear();
data.swap(v.data); // Could also write: data = std::move(v.data);
}
return *this;
}
// Accessor returning a rvalue:
int operator[]( const size_t i ) const
{
return data[i];
}
// Accessor returning a lvalue:
int& operator[]( const size_t i )
{
return data[i];
}
// The implementation of operator+ that this example is demonstrating:
vec4i operator+(const vec4i& v) const
{
vec4i temp;
for ( size_t i = 0; i < size; ++i )
temp[i] = data[i] + v[i];
/* Since we provided a move constructor and move assignment above, the
* compiler should be able to optimize away this copy!
*/
return temp;
}
// Returns a vector with the sign of each elemeent flipped:
vec4i operator-(void) const
{
// The dummy second parameter selects the programmatic constructor.
return vec4i( *this, op_opposite() );
}
// Now we use the constructor above to construct the return value:
vec4i operator-(const vec4i& v) const
{
// Function object wrapping a-b, initialized once:
static const binary_fun subtract = std::minus<int>();
// Create the returned temporary object in place.
return vec4i( *this, v, subtract );
}
};
std::ostream& operator<<( std::ostream& os, const vec4i& v )
// Serialize our vector.
{
return os <<
'<' << v[0] << ',' << v[1] << ',' << v[2] << ',' << v[3] << '>';
}
int main(void)
{
const vec4i u = {0,0,0,1}, v = {1,0,0,0};
// Output: "<1,0,0,1>"
cout << u+v << endl;
// Output: "<-1,0,0,1>"
cout << u-v << endl;
// output: "<-1,0,0,1>"
cout << -(v-u) << endl;
return EXIT_SUCCESS;
}