[C++] Operator= problem

Started by
5 comments, last by Tjaalie 16 years, 6 months ago
Dear Thread reader, I'm making a varriable class for my scripting engine and now I want it to dynamicaly convert between types. I though of this:

class Script
{
public:
  bool Load(char* file);

  template <class Type> Type VarData(char* section, char* name);
};

In order to do this I do the following in the var data function

//First search for the right var wich is a class of the type Var:D
//We will pretend that the varriable Vars contains the right var.

Type Data = Vars;
return Data;

Now in order for this to work I though I needed to override the operator= in the class Var, I tried it serveral ways but it doesn't work. It keeps telling me that it can't convert Var to float while I have implented the operator like this:

class Var
{
public:
  float operator=(const Var &var);
};

float Var::operator=(const Var &var)
{
  return var.m_FloatData;
}

What is wrong with this code? Tjaalie,
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
Advertisement
class Var{public:  float operator=(const Var &var);};


That declares an assignment operator that takes two Var instances and returns a float. It doesn't declare an assignment operator that assigns Vars to floats.
Hi
I think you have a mixup with how the operator works.
For this code to work, the variable type for Data should be class Var. Other then that, the operator has to store the given data in one of its members. Maybe you want something like that:

Var& Var::operator=(const Var &var)
{
this->m_FloatData = var.m_FloatData;
return *this;
}
no it must return a float, I want to do stuff like this:
float SomeFloat = Vars;
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
imagine a cituation

struct asd{int a;int b;}asd a, b, c(3,4);a=b=c;


why is this possible?

because the operator "asd& operator=(asd&)" looks like:

asd& operator=(asd& copy){a = copy.a;b = copy.b;return *this;}


there is always a hidden parameter "this" in C++ member functions. It is a pointer to the main object. And is the sape write "this->a" than "a" ('cause "this" is "default").
In the operator= you should copy all member variables (making work "a=b")and then return a reference to the object (alowing a=b=c).

Oh, and it's a good choice checking if you're not doing "a=a"
asd& operator=(asd& copy){if(this!= copy ){a = copy.a;b = copy.b;}return *this;}

it's avout pointers...


[opinion]
If you don't control C++ don't write a script engine. It's one of the hardest thinks you can do.
[/opinion]
Quote:Original post by Tjaalie
no it must return a float, I want to do stuff like this:
float SomeFloat = Vars;


What you are trying to do is to define float::operator=(const Var&), but that is not possible. You have to use another approach: Convert Var to a float.

class Var{  float operator float() const  {    return m_FloatData;  }};


Note that in general, such implicit conversions should be avoided, since they make code harder to understand.
You would be better off using an accessor, IMHO.
-- Top10 Racing Simulation needs more developers!http://www.top10-racing.org
Thanks that works, with accesors do you mean member functions with Get and Set?
This code isn't used by the users its used so I can do this in a template function:

float Data = Script.GetVarData<float>("Options", "Scale");


Anyway, rate++;
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };

This topic is closed to new replies.

Advertisement