Sign in to follow this  

[C++] Operator= problem

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++;

Share this post


Link to post
Share on other sites

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this