ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have 2 struct variables in a cpp file. I cannot use operator== for the struct.
Code:
#include <stdlib.h>
#include <stdio.h>
typedef struct myStruct_t
{
int i;
int j;
};
int main()
{
myStruct_t a, b;
a.i = b.i = 2;
a.j = b.j = 3;
if (a == b)
{
printf("a == b\n");
}
else
{
printf("a != b\n");
}
return 0;
}
Does the struct have the default behavior for the operator==?
I compiled it useing "g++ t.cpp" and got the following error message
t.cpp: In function `int main()':
t.cpp:16: error: no match for 'operator==' in 'a == b'
exactly, no == is defined for structs... thats a good example from AdaHacker, except you should return int not bool..
IMO, you should use a class here, Im not a big fan of overloading at global level... structs are for data, class is for data and operations on that data which this would qualify as.. I would do somthing like this..
Code:
class Foo
{
public:
Foo(){
x=0;
y=0;
}
void setX(int xin){ x=xin; }
void setY(int yin){ y=yin; }
int getX()const{ return x; }
int getY()const{ return y; }
int operator==(const Foo& right)const{
return (x == right.getX()) && (y == right.getY());
}
private:
int x;
int y;
};
The reason, that I use the struct instead of the class, is for the public member variables and the light weight. If I use classes, my brain will ask me to write the set()/get() for the variables.
So what's the other difference between the struct and the class. I know all members is public for a struct. Anything elese? Thank you. And I guess a struct must has a default constructor and the default behavior for optertor=. So what's that?
The reason, that I use the struct instead of the class, is for the public member variables and the light weight. If I use classes, my brain will ask me to write the set()/get() for the variables.
So what's the other difference between the struct and the class. I know all members is public for a struct. Anything else? Thank you. And I guess a struct must has a default constructor and the default behavior for operator=. So what's that?
>> So what's the other difference between the struct and the class.
Mainly personal preference.. a *C++* struct and class are not different except for the public/private defaults you have mentioned. Other than that there is nothing that I know of.. Honestly I dont know what the difference in overhead, it would seem like a struct is simpler, and would have less... however they can act identical.. so there may not be a difference...
I use classes because the feel more C++-ey to me.. When I think encapsulation I think class, when I think about defining a simple type like coordinates or somthing, I think struct.. The code that follows is some example.. I knew that in theory there was no difference in the two, but I had never experimented. I killed a few minutes this evening to find out for myself if there were any differences.. And the answer I proved was, no.. atleast not at the level I got to in this example... Check it out.. it does compile as is....
Code:
#include <iostream>
using namespace std;
/*
output
----------
FooBar:: outFooBar()
Bar:: outBar()
Foo:: out()
FooTwo:: outTwo()
Bar:: overloaded comparison
FooBar:: overloaded comaprison
*/
class Foo
{
public:
void outFoo(){ cout << "Foo:: out()" << endl; }
};
struct FooTwo
{
void outTwo(){ cout << "FooTwo:: outTwo()" << endl; }
};
struct Bar : public Foo, public FooTwo
{
void outBar(){
cout << "Bar:: outBar()" << endl;
outFoo();
outTwo();
}
int operator==(const Bar& r) const{
cout << "Bar:: overloaded comparison" << endl;
return 0;
}
};
class FooBar : public Bar
{
public:
void outFooBar(){
cout << "FooBar:: outFooBar()" << endl;
outBar();
}
int operator==(const FooBar& r) const{
cout << "FooBar:: overloaded comaprison" << endl;
return 0;
}
};
int main(int c, char** v)
{
FooBar fbA = FooBar();
FooBar fbB = FooBar();
Bar barA = Bar();
Bar barB = Bar();
fbA.outFooBar(); // test all of our print messages....
barA==barB; // test that comparison op in the struct
fbA==fbB; // ..
return 0;
}
<edit> removed the virtual function example.. it didnt illustrate what i was thinking quite the way i was thinking it.. maybe next time</edit>
structs are super super helpful in C, but when it comes to C++ as it was stated, its a personal
preference between classes and structs. In C, structs let you have arrays that can be
dynamically allocated and all that good stuff, which you can't do with classes. In C++, its a
lot more flexible, kind of like java.
When you wrote your code, comparing a structure with more than one element using an ==
operator immediately sparked a warning, but I would if you could do it if it was a structure
with just one element. To compare structures, you usually need to write a function that
compares each element of the structure to each element of another true, and then return a
boolean in the case of C++ or an integer denoting true or false (1 or 0) in C.
If you get some time, try a structure with one element using the == operator, id like to see
the results and some people's feedback.
To compare structures, you usually need to write a function that
compares each element of the structure to each element of another true, and then return a
boolean in the case of C++ or an integer denoting true or false (1 or 0) in C.
Thats exactly what you are doing with the operator==.. What I found out here is that a *C++* struct and *C* struct are two very different things... While C++ has given the struct the *same abilities* as a class, total encapsulation.. Like you said, in C you are forced to write other functions, as there is no concept of a member function.. I have never tried to put a function ptr into a struct.. maybe you could rig up somthing like that?? that would be some ugly code for sure.. anyhow I made a modification or two on the code...
here is a struct showing the use of the == operator with the multi member value struct.. its like a C struct on steroids
Code:
struct Fooey : public Bar
{
Fooey():a(5),b(5){}
// normal compare op you would see for objects
int operator==(const Fooey& r) const{
return ((a == r.a) && (b == r.b));
}
// illustration only.. very anti intuitive!
int operator==(int r) const{
return a==r);
}
int a, b;
};
here are a couple extra comparisons for main
Code:
Fooey fooeyA;
Fooey fooeyB;
if(fooeyA==fooeyB) cout << "Fooey A and Fooey B *are* equal" << endl;
if(fooeyA==5) cout << "The value a in Fooey A is " << fooeyA.a << endl;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.