Jump to content
  • Advertisement
Sign in to follow this  
SirSmokey

Assigning a struct to a class

This topic is 4454 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

How exactly would you go about assigning a struct to a class? Example: struct SimpleStruct { int data; int moredata; } class MyStructClass { int data; int moredata; } SimpleStruct struct; MyStructClass object; struct = object; The assignment operator can only be overloaded in a class and it's always the left hand operand. So how can make something like this work?

Share this post


Link to post
Share on other sites
Advertisement
Since a "class" is availible you must be using C++...

Structs in C++ are the same as classes, only their default protection is public, not private.

Anyways, you can't just assign different classes/structs to each other. You must define an operator for it:

class b {
public:
int i;
};

struct a {
int i;

a &operator =(const b &o) {
i = o.i;
return *this;
}
};

b B;
a A;
A = B;


Same goes the other way.

Share this post


Link to post
Share on other sites
Thanks for your reply but..

I failed to explain my whole situation (for this I apolagize).

Yes I am working with C++, and I neglected to mention that the struct is one from an API so I don't have access to modify it so adding an overloaded operator to it is not an option.

Again sorry for the lack of detail.

Share this post


Link to post
Share on other sites
Quote:
Original post by SirSmokey
Thanks for your reply but..

I failed to explain my whole situation (for this I apolagize).

Yes I am working with C++, and I neglected to mention that the struct is one from an API so I don't have access to modify it so adding an overloaded operator to it is not an option.

Again sorry for the lack of detail.


Make a function to do it for you.

void assign( API_Bar &bar, const Foo &foo )
{
bar.member = foo.member;
}

I'm pretty sure you cannot make it so that it is avaiable as an operator overload without modifing the source.

Share this post


Link to post
Share on other sites
You could always create a wrapper class that contains the original struct and passes along regular calls to it, handling any special ones in the wrapper.

Share this post


Link to post
Share on other sites
Couldn't you just add a global overloaded operator?



SimpleStruct& operator=(SimpleStruct& s, MyStructClass& c)
{
s.data = c.data;
s.moredata = c.moredata;
}



Any particular reason why you couldn't just add that into your own code? I'm assuming that's the only place it will be used.

Share this post


Link to post
Share on other sites
Endar: YOu can't overload the assignment operator outside of a class and since the "SimpleStruct" isn't part of my code but a struct from an API I can't modify it.

Share this post


Link to post
Share on other sites
You might want to consider the function or the wrapper method.
For the wrapper method, you might want to use "operator Wrapper::API_X*()" and "operator Wrapper::API_X&()" (though I'm not entirely sure if the latter will work).

Share this post


Link to post
Share on other sites
You can inherit from their struct and just add functions without adding any data members.

Share this post


Link to post
Share on other sites
You could use something like the following if your struct and class have the exact same members in the exact same order, or maybe if the first few members of your struct are the exact same as the entire class.

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

#define P(t) std::cout << t
#define I(v) std::cin >> v
#define transferdata(target, source) memcpy(&target, &source, sizeof(source));

class cvar
{
public:
int a;
float b;
};

typedef struct
{
int a;
float b;
}svar;

int main()
{
cvar v1; // our class
svar v2; // our struct
v1.a = 2; // set the values of our class
v1.b = 3.0f;
transferdata(v2, v1); // transfer data from our class to our struct
P(v2.a); // display a variable from our struct. Answer should be 2.
getch(); // wait for a keypress
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!