overloaded operators

here's a snippet, i'll give some context after:

========================================

struct boo
{  
   char *operator=(boo *a);
   int a;
};

char *boo::operator=(boo *a)
{
   return (char *)a;
}

void main( void )
{
   boo *a;
   char *b;

   b = a;
}

==============================

ok, so this code doesnt work... It says "error C2440: '=' : cannot convert from 'struct boo *' to 'char *'"

what i'm trying to do is exactly what it says. I want to change the code above main to make main work. I have a few hundred applications and I don't want to have to go and change every one of them. Furthermore, the operator will be more than just a type case, but the main thing is that it's input is a struct pointer and output is a char pointer. I can write the meat of the function once this works but I have no idea how to get it working properly :(

any help would be greatly appreciated thanks :)
Jimmy231Asked:
Who is Participating?
 
jkrCommented:
That won't work like that for pointers.

>>but instead of typecasting EVERYWHERE I would like to be able to make come kind of operator

There's a way easier solution - since you can provide function overloads in C++, just use

int function(boo* param, int other_param) {

    return function((char*) param, other_param);
}

which will do the conversion at one single place.
0
 
jkrCommented:
>>char *operator=(boo *a);

That does not really make sense - did you mean

struct boo
{  
   operator char*() {return (char*) this;}
   int a;
};


void main( void )
{
   boo a;
   boo* p;
   char *b;

   b = a;
   b = *p;
}

?
0
 
Jimmy231Author Commented:
no, I know it's a little stupid but here's whats happening.
i've changed a typedef from a char array to a struct pointer (for my own reason) and so now there are a whole bunch of calls to a function like this one

int function(char* param, int other_param)

that ideally I want to change to

int function(boo* param, int other_param)

but instead of typecasting EVERYWHERE I would like to be able to make come kind of operator to convert from boo* to char*. i'm not converting to or from the base structure.

I hope this clarifies my question.


thanks for the speedy reply :)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Jimmy231Author Commented:
That wrapper function does exaclty what I want, but it's not just 1 function that I need that done for though, there are a bunch of functions like that and I want to avoid having to change them all... is there any other way to do it without having to change all the functions?
0
 
jkrCommented:
>>is there any other way to do it without having to change all the functions?

Not that easily I'm afraid. The choices basically are to provide overloads or touch the parameters in the funciton calls.
0
 
rstaveleyCommented:
To minimise edits, you could use an evil macro.

e.g.

#define function(param, other_param) \
        function_impl(reinterpret_cast<char*>(param), other_param)

Then you'd need to rename only the function to function_impl, and you could leave the calling code intact.
0
 
rbvoigtCommented:
Basic misunderstanding of typedef?

I guess you had
typedef char* boo;
which you have replaced by
struct boo { ... };

Your functions should have been declared like:
grow_a_boo(boo& the_boo);

which would work before and after the change.  But it sounds like you didn't use proper encapsulation, and defined all your functions as
grow_a_boo(char*& the_boo);

You should use an interactive search-and-replace and change all your variables representing boo objects to the boo type now, because the longer you let it go, the worse things will get.

jkr's operator char*() {return (char*) this;} will work, but this is a dangerous cast.

What I think you were aiming for was:

struct boo
{  
   friend char* operator=(char *&, boo&);
   int a;
};

char* operator=(char*& p, boo& a)
{
   return p = (char *)&a;
}

which redefines operator = to allow putting a boo in a char*, allowing your first example to run, however jkr's user-defined cast will work for function calls and this operator = won't.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.