You are not logged in.

#1 2012-12-23 17:24:48

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

Operator overloading * (pointers) [C++]

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

#2 2012-12-23 17:47:10

thestinger
Package Maintainer (PM)
From: Toronto, Canada
Registered: 2010-01-23
Posts: 478

Re: Operator overloading * (pointers) [C++]

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

#3 2012-12-23 18:10:34

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

Re: Operator overloading * (pointers) [C++]

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

Thanks.

Offline

#4 2012-12-23 18:38:01

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

Re: Operator overloading * (pointers) [C++]

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

#5 2012-12-23 18:41:47

thestinger
Package Maintainer (PM)
From: Toronto, Canada
Registered: 2010-01-23
Posts: 478

Re: Operator overloading * (pointers) [C++]

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

#6 2012-12-23 20:13:24

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

Re: Operator overloading * (pointers) [C++]

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

Offline

#7 2012-12-29 17:55:33

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

Re: Operator overloading * (pointers) [C++]

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

Offline

#8 2013-01-01 00:04:32

thestinger
Package Maintainer (PM)
From: Toronto, Canada
Registered: 2010-01-23
Posts: 478

Re: Operator overloading * (pointers) [C++]

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

#9 2013-01-01 03:59:59

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

Re: Operator overloading * (pointers) [C++]

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

Board footer

Powered by FluxBB