You are not logged in.

- Topics: Active | Unanswered

**more-ingo****Member**- Registered: 2012-08-29
- Posts: 6

Hi Archusers:

I have a big and big questions, when I overload an operator (such as multiply) this can return a pointer, but, the overload can only be with "objects" in the stack.

An example:

If I have "Matrix class":

```
class Matrix{
//Field, Getters, setters and more
Matrix *operator*(Matriz *m);
};
```

And I initialize two pointers and multiply these two:

```
Matrix *m1 = new Matrix();
Matrix *m2 = new Matrix();
Matrix *m3 = m1 * m2;
```

But this code is wrong, I only can multiply if and only if "m1" is not pointer. The correct code is:

```
Matrix m1 = new Matrix();
Matrix *m2 = new Matrix();
Matrix *m3 = m1 * m2;
```

Is there any way of multiply pointer-by-pointer and return another pointer?

Sorry for my English.

Greetings.

Offline

**thestinger****Trusted User (TU)**- From: Toronto, Canada
- Registered: 2010-01-23
- Posts: 478

more-ingo wrote:

Hi Archusers:

I have a big and big questions, when I overload an operator (such as multiply) this can return a pointer, but, the overload can only be with "objects" in the stack.

An example:If I have "Matrix class":

`class Matrix{ //Field, Getters, setters and more Matrix *operator*(Matriz *m); };`

And I initialize two pointers and multiply these two:

`Matrix *m1 = new Matrix(); Matrix *m2 = new Matrix(); Matrix *m3 = m1 * m2;`

But this code is wrong, I only can multiply if and only if "m1" is not pointer. The correct code is:

`Matrix m1 = new Matrix(); Matrix *m2 = new Matrix(); Matrix *m3 = m1 * m2;`

Is there any way of multiply pointer-by-pointer and return another pointer?

Sorry for my English.

Greetings.

You really shouldn't be doing overloads for by-pointer like that or dynamically allocating the return value at all. The memory allocation should be internal to the Matrix class - there should be no usage of 'new' outside of it. The overload should be a free function (not a member function) taking two Matrix arguments by const reference and making another.

Make the operator* implementation allocate a new one and return by-value - that's a move, not a copy (but the move will be optimized out anyway). It's really important to never be doing things like fclose, free, delete, etc. outside of low-level details in a destructor - otherwise your code is broken and will leak resources if an exception is thrown.

*Last edited by thestinger (2012-12-23 17:48:46)*

Offline

**more-ingo****Member**- Registered: 2012-08-29
- Posts: 6

Sorry for my novice knowledge in C++ and programming in general. ¿Can you put an liitle example of how you would do?

Thanks.

Offline

**gs93****Member**- Registered: 2012-10-19
- Posts: 16

```
Matrix::Matrix(const Matrix &other)
{
// copy constructor
}
Matrix &Matrix::operator*=(const Matrix &other)
{
// calculate
return *this;
}
// free function
const Matrix operator*(const Matrix &lhs, const Matrix &rhs)
{
return Matrix(lhs) *= rhs;
}
```

Thats how i learned to do this.

Offline

**thestinger****Trusted User (TU)**- From: Toronto, Canada
- Registered: 2010-01-23
- Posts: 478

gs93 wrote:

`Matrix::Matrix(const Matrix &other) { // copy constructor } Matrix &Matrix::operator*=(const Matrix &other) { // calculate return *this; } // free function const Matrix operator*(const Matrix &lhs, const Matrix &rhs) { return Matrix(lhs) *= rhs; }`

Thats how i learned to do this.

Looks good, with the nitpick that you'd want to take lhs by-value in C++11, if you're going to copy it (which lets move-semantics kick in when it can).

You would probably allocate the memory with new[] in the constructor and free it with delete[] in the destructor, and then you just need copy constructor + move constructor + copy/move assign (which can be 1 method - taking it by-value, or two methods with one by const-reference and one taking an rvalue).

Can be tricky to get the exception-safety semantics right for those - but you can use a unique_ptr<T[]> as a member which makes it really easy (if you do that - I think you'll even be fine with the default copy/move/copy-assign/move-assign and destructor).

*Last edited by thestinger (2012-12-23 18:44:42)*

Offline

**gs93****Member**- Registered: 2012-10-19
- Posts: 16

Oh.. Thanks, didn't knew the move semantic.

Offline

**more-ingo****Member**- Registered: 2012-08-29
- Posts: 6

Thanks archers, I need to study and practique C++. ¿Any C++ recommended book? If the book is updated (C++ 11), better.

Offline

**thestinger****Trusted User (TU)**- From: Toronto, Canada
- Registered: 2010-01-23
- Posts: 478

more-ingo wrote:

Thanks archers, I need to study and practique C++. ¿Any C++ recommended book? If the book is updated (C++ 11), better.

C++ Primer (5th Edition) is supposed to be good, and it was heavily updated for C++11.

Offline

**Trent****Member**- From: Baltimore, MD (US)
- Registered: 2009-04-16
- Posts: 986

I don't have a book to recommend, but I have found ACCU's book reviews to be quite accurate and valuable when looking for programming books. *C++ Primer*, sure enough, gets a "Highly recommended".

Offline