2

My Question: I'm trying to overload the assignment operator for pointers to two different classes. Here is an example:

dc.h:

#ifndef DC_H_
#define DC_H_

#include "ic.h"

class dc {

double d;
char c;

public:

dc(): d(0), c(0) { }

dc(double d_, char c_): d(d_), c(c_) { }

dc* operator=(const ic* rhs);

~dc() { }

};

#endif /* DC_H_ */

class ic.h:

#ifndef IC_H_
#define IC_H_

class ic {

int i;
char c;

public:

ic(): i(0), c(0) { }

ic(int i_, char c_): i(i_), c(c_) { }

~ic() { }

};

#endif /* IC_H_ */

dc.cpp:

#include "dc.h"

dc* dc::operator=(const ic* rhs) {
d = rhs->i;
c = rhs->c;  
return this;
}

ic.cpp:

#include "ic.h"

main.cpp:

#include "dc.h"
#include "ic.h"

#include<iostream>

int main() {

dc DC;
ic IC;

dc* dcptr = &DC;
ic* icptr = &IC;

dcptr = icptr;

return 0;
}

I get the following error message:

error: cannot convert 'ic*' to 'dc*' in assignment

All this works if I do it with references instead of pointers. Unfortunately since I would like to use pointers to ic and dc as members in another class I cannot use references since references as members have to be initialized and once initialized they cannot be changed to refer to another object. I'd like to be able to make arithmetic operations with ic and dc e.g.:

dc *d1, *d2, *d3;
ic *i1, *i2, *i3;
*d1 = (*d1)*(*i1) + (*i2)*(*d2) - (*d3)*(*i3);

However I want my code to look nice and don't want to have (*)*(*) all over the place. Instead something like this:

d1 = d1*i1 + i2*d2 - d3*i3;

This is the reason why I'd like to do this. Please let me know if this is possible at all. To me it seems that the compiler wants to call the default pointer to pointer assignment instead of the overloaded one.

Thanks for your help in advance!

5
  • Your reasoning for using pointers is flawed. As is your reasoning for not using references. You would achieve what you are trying by using references. Commented Feb 19, 2013 at 1:13
  • 1
    You cannot overload operators for built-in types. Commented Feb 19, 2013 at 1:15
  • Hi Drew, thanks for your quick response. So you say that it is possible to use references as members? The book I'm using says the following about references: 1. A reference must be initialized when it is created. (Pointers can be initialized at any time.) 2. Once a reference is initialized to an object, it cannot be changed to refer to another object. (Pointers can be pointed to another object at any time.) 3. You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage. If you know a wrk arnd. Could you pls pt me to sw! Tx! Commented Feb 19, 2013 at 1:17
  • Hi Kerrek, Thanks for your quick response. I'm trying to overload = operator for ic and dc these are classes I've created. Do you have any other suggestions? Thanks! Commented Feb 19, 2013 at 1:23
  • In my first comment I mean: So you say that it is possible to use references as members without initializing or initializing to some garbage and then changing that? Thanks! Commented Feb 19, 2013 at 1:25

1 Answer 1

10

You cannot overload operators for pointers.

One option, if you want to stick with operator overloading is to make a pointer wrapper object, an object that contains a pointer to the object - essentially a smart pointer, and overload the operators of that object.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Karthik, Thanks for your answer. That's a good idea! Will give it a try. One question though: if I overload = operator let's say like this: ic* operator=(ic* i) { copies data from i to left hand side; } That actually works. So you can overload the assignment operator for pointers. To me it seems the issue is that lhs and rhs are not the same type. (Unfortunately I cannot yet upvote b/c I don't have enough rep.)
Hi! So I've implemented your answer in the project I'm working on and it works perfectly! I hope someone with enough reputation comes along and upvotes your answer!
@George you can mark the answer as accepted if it helped you. It is the green tick(?) to the left of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.