LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2004, 02:55 PM   #1
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
C: Add an operator to a struct


Is there anyway I can achieve make an operator (eg +) for a struct I've defined myself. For example:
Code:
typedef struct complex {
	double a;
	double b;
};

main(){
struct complex foo,bar,foobar;
foo.a = 4; foo.b = 7; //foo = 4 + 7j
bar.a = -3; bar.b = 2; //bar = 2j - 3
foobar = foo+bar; //foobar = 1 + 9j
}
I know that it can be done in C++, but I don't fancy learning the whole classes making thing just for this.

Thank you.

EDIT: Also, if there a clever way to set a struct, like foo = {4,5}; instead of invidually setting both members. I tried the above, but DJGPP didn't like it when not used during the original creation (struct complex foo = {4,7}; ).

Last edited by drigz; 09-10-2004 at 03:03 PM.
 
Old 09-10-2004, 03:31 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
you can't do operator overloading in C; you'll have to write a function like struct complex add_complex(const struct complex *x, const struct complex *y)

afaik, the only way to assign all the members in a struct after it's initialized is to set it equal to another struct of the same type, e.g. foo = bar; I've found this annoying too.
 
Old 09-10-2004, 03:47 PM   #3
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
i've just had to start compiling it with c++ so i can overload some of my functions, so i'm prepared to try and do it a c++ way - which way would require least new learning, since i know nothing about classes?
 
Old 09-10-2004, 03:50 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
As an aside -

Looks like you're doing complex arithmetic.

C99 supports a large library of complex functions as well as the complex datatype. Most flavors of Linux support it.
 
Old 09-10-2004, 03:57 PM   #5
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
Really? I figure it will be more educational (I will still need to know this for my exam...) and the theory writing a program will teach me it and also help me do homework quicker.
 
Old 09-10-2004, 04:07 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You don't have to deal with classes in C++. You can just use your code and add operator overriding.
Sidenote: I'd rather just use functions: add_complex() etc.
 
Old 09-10-2004, 04:22 PM   #7
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
yeh but i've got some pretty complex functions for some of this stuff and id rather the shorter and more intuitve version. thanks tho.
 
Old 09-11-2004, 06:44 PM   #8
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
Done it. Found a good guide here: http://www.cplusplus.com/doc/tutorial/tut4-2.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
operator== for struct lordofring Programming 10 11-21-2005 12:09 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration