View unanswered posts | View active topics It is currently Sun Dec 21, 2014 3:28 pm






Reply to topic  [ 5 posts ] 
Operator overloading for a struct 
Author Message
Rookie

Joined: Fri Nov 22, 2013 5:40 pm
Posts: 2
Post Operator overloading for a struct
Is it possible to do operator overloading in RobotC? For example we have a struct:
Code:
typedef struct{
float x;
float y;
float z;
} vector;
And we would like to be able to run:
Code:
vector a, b;
a.x = 1;
a.y = 2;
a.z = 3;

b=a;
And have all the x,y,z values copied over. Is this possible in RobotC?

Thanks in advance,

- Alex


Fri Nov 22, 2013 5:47 pm
Profile
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Re: Operator overloading for a struct
No, I don't think this is possible in RobotC (or ANSI C, even).

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Fri Nov 22, 2013 6:52 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1371
Post Re: Operator overloading for a struct
lemiant wrote:
Is it possible to do operator overloading in RobotC? For example we have a struct:
Code:
typedef struct{
float x;
float y;
float z;
} vector;
And we would like to be able to run:
Code:
vector a, b;
a.x = 1;
a.y = 2;
a.z = 3;

b=a;
And have all the x,y,z values copied over. Is this possible in RobotC?

Thanks in advance,

- Alex

That's not operator overloading. That's just structure assignment. This should be allowed. Since you already have the code written, why can't you just compile the code and check it out. To verify if it indeed works, add some nxtDisplayText statements in the code such as:
Code:
typedef struct
{
    float x;
    float y;
    float z;
} vector;

task main()
{
    vector a, b;
    a.x = 1.0;
    a.y = 2.0;
    a.z = 3.0;
   
    b = a;
   
    nxtDisplayTextLine(1, "b.x=%f", b.x);
    nxtDisplayTextLine(2, "b.y=%f", b.y);
    nxtDisplayTextLine(3, "b.z=%f", b.z);
   
    while (true)
    {
        wait1Msec(100);
    }
}


Sat Nov 23, 2013 8:17 am
Profile
Rookie

Joined: Fri Nov 22, 2013 5:40 pm
Posts: 2
Post Re: Operator overloading for a struct
Good idea with the testing. It seems that structs can be passed as parameters to functions, but you cannot do a=b as I was hoping. Does that make sense?


Sat Nov 23, 2013 3:08 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1371
Post Re: Operator overloading for a struct
Hmm, you are right. It looks like structure assignment doesn't work. Then try this:
Code:
typedef struct
{
    float x;
    float y;
    float z;
} vector;

task main()
{
    vector a, b;
    a.x = 1.0;
    a.y = 1.0;
    a.z = 1.0;
   
    while (true)
    {
        a.x += 1.0;
        a.y += 1.0;
        a.z += 1.0;
        memcpy(&b, &a, sizeof(a));
//        b = a;
   
        nxtDisplayTextLine(1, "b.x=%f", b.x);
        nxtDisplayTextLine(2, "b.y=%f", b.y);
        nxtDisplayTextLine(3, "b.z=%f", b.z);
   
        wait1Msec(100);
    }
}


Sat Nov 23, 2013 4:51 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.