[c-prog] query on prefix and post fix operator overloading
Hi, I have a small query. why we need to return reference in prefix operator overloading. Because, when we call as ++obj , the operator ++ method will be called and we increment the values using this pointer. I will paste the code. Please let me know where I failed. #includestdio.h #includeconio.h #includeiostream using namespace std; class mydata { private: int x,y; public: mydata() { coutI m i9n consendl; } mydata(int a,int b):x(a),y(b) { coutI m in parmendl; } void sow() { coutx = x y = yendl; } mydata operator +(mydata obj) { x=x+obj.x; y=y+obj.y; return *this; } void operator ++() { this-x++; this-y++; } friend mydata operator +(int val,mydata obj) { obj.x=val+obj.x; obj.y=val+obj.y; return obj; } mydata(mydata obj) { this-x=obj.x; this-y=obj.y; } mydata operator =(mydata obj) { if(this!=obj) { this-x=obj.x; this-y=obj.y; } return *this; } }; void main() { mydata obj1(2,4); obj1.sow(); mydata obj2(4,7); obj2.sow(); obj1=obj1+obj2; obj1.sow(); #ifdef _DEBUG __asm{int 3} #endif ++obj1; obj1.sow(); mydata obj3=2+obj1; obj3.sow(); } GopiKrishna Komanduri Software engineer [EMAIL PROTECTED] --- On Thu, 27/11/08, Gopi Krishna Komanduri [EMAIL PROTECTED] wrote: From: Gopi Krishna Komanduri [EMAIL PROTECTED] Subject: [c-prog] query on Exception handling (in constructor and desctructor) To: c-prog@yahoogroups.com Date: Thursday, 27 November, 2008, 10:24 AM Hi, I have a query related to exceptional handling. I pasted the code I tried following this mail. My intention is , what happens in a sistuation when any exception is caught in main , so while destructing the objects till that instant again if any exception is caught (stack unwinding concept). similary in constructor. For constructor I know that the desctructors of the sub objects will be called . But how that exception is handled . #includestdio. h #includeconio. h #includeiostream using namespace std; class var1 { public: var1() { coutI m in var1 concendl; } ~var1() { coutI m in var1 descendl; } }; class MyCls { public: var1 *obj; MyCls():obj( NULL) { obj=new var1(); coutI m in consendl; } ~MyCls() { coutI m in descendl; throw excepto; delete obj; coutDeleted objendl; } }; void main() { try { MyCls obj; coutObj Createdendl; throw error; coutObj after throwing errendl; } catch(char *x) { coutxendl; } catch(...) { coutI m in lastendl; } } GopiKrishna Komanduri Software engineer Hyderabadgopikomand [EMAIL PROTECTED] com --- On Tue, 25/11/08, Brett McCoy [EMAIL PROTECTED] com wrote: From: Brett McCoy [EMAIL PROTECTED] com Subject: Re: [c-prog] Exception handling To: [EMAIL PROTECTED] com Date: Tuesday, 25 November, 2008, 9:29 PM On Tue, Nov 25, 2008 at 9:10 AM, crystalcat_75 crystalcat_ 75@ yahoo.co. in wrote: Following program is showing an error while running in turbo c : undefined symbol for try statement missing after try . I don't think Turbo C supports exceptions. You need to upgrade to a modern compiler like Dev-C++ or MS Visual C++ Express (they are free) and not use something that is 15+ years old. -- Brett - - - - - - In the rhythm of music a secret is hidden; If I were to divulge it, it would overturn the world. -- Jelaleddin Rumi Add more friends to your messenger and enjoy! Go to http://messenger. yahoo.com/ invite/ [Non-text portions of this message have been removed] Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/ [Non-text portions of this message have been removed]
[c-prog] Re: query on Exception handling (in constructor and desctructor)
Gopi Krishna Komanduri [EMAIL PROTECTED] wrote: Hi, I have a query related to exceptional handling. I pasted the code I tried following this mail. My intention is , what happens in a sistuation when any exception is caught in main , so while destructing the objects till that instant again if any exception is caught (stack unwinding concept). http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3 -- Peter
[c-prog] Re: query on prefix and post fix operator overloading
Gopi Krishna Komanduri [EMAIL PROTECTED] wrote: I have a small query. why we need to return reference in prefix operator overloading. http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq- 13.14 http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3 -- Peter
Re: Res: [c-prog] Re: integer promotions
--- peternilsson42 wrote: -Wconversion Warn for implicit conversions that may alter a value. ... Integral promotions don't alter the value. Maybe you need -Wsign-conversion gcc -Wall -Wsign-conversion problem.c -o problem cc1: error: unrecognized command line option -Wsign-conversion
Re: Res: [c-prog] Re: integer promotions
--- Thomas Hruska wrote: Try compiling your code as C++ and see if there is a difference. C++ compilers tend to generate a lot more warnings as the language is, generally-speaking, more strict. [EMAIL PROTECTED] ~/programming/c++/problem $ ls -la total 10 drwxr-xr-x+ 2 root None 4096 Nov 27 22:47 . drwxr-xr-x+ 11 root None 4096 Nov 27 22:35 .. -rw-r--r-- 1 root None 69 Nov 27 22:37 Makefile -rw-r--r-- 1 root None 261 Nov 27 22:46 problem.cpp [EMAIL PROTECTED] ~/programming/c++/problem $ cat problem.cpp #include climits #include iostream using namespace std; int main (void) { unsigned short int a; unsigned long long int b, c; a = USHRT_MAX; b = (a*a); c = ((unsigned int)a*(unsigned int)a); cout Why hex b != c ?\n; return 0; } [EMAIL PROTECTED] ~/programming/c++/problem $ cat Makefile problem : problem.cpp g++ -Wall -Wconversion problem.cpp -o problem [EMAIL PROTECTED] ~/programming/c++/problem $ make g++ -Wall -Wconversion problem.cpp -o problem [EMAIL PROTECTED] ~/programming/c++/problem $ ./problem.exe Why fffe0001 != fffe0001 ?
[c-prog] appliction crashing
Hi, While I am trying to debug the following code , it is executing as expected. But when trying to run with out debugging (debug version itself) , the applicaton is crashing. May I know why is it so. #include stdio.h #include stdlib.h struct Tree { int data; int priority; struct Tree* lChild; struct Tree* rChild; }; struct Tree *root = NULL; struct Tree *prev = NULL; void heapInsert(struct Tree *tree, int key, int priority){ struct Tree *node = (struct Tree*)malloc(sizeof(struct Tree*)); node-data = key; node-priority = priority; node-lChild = NULL; node-rChild = NULL; if (root == NULL){ root = node; } else { struct Tree *dummy = (struct Tree*)malloc(sizeof(struct Tree*)); dummy = root; if (dummy-priority = node-priority){ root = node; root-lChild = dummy; } while (dummy-priority node-priority){ if (dummy-lChild == NULL){ dummy-lChild = node; dummy = node; } else if (dummy-rChild == NULL) { dummy-rChild = node; dummy = node; } else { prev = dummy; dummy = dummy -lChild; } } if (prev != NULL){ prev-lChild = node; node-lChild = dummy; prev = NULL; } /*printf(You are accessing the dummy root with pr %d\n, dummy-priority); printf(You are now trying to insert a second node.\n); printf(First node still with priority %d, root-priority);*/ } } void treeTraversal(struct Tree * tree){ printf(Data: %d\n, tree-data); printf(Priority: %d\n, tree-priority); printf(LChild points to: %#x\n, tree-lChild); printf(RChild points to: %#x\n, tree-rChild); } int main(void){ struct Tree *p = (struct Tree*)malloc(sizeof(struct Tree)); heapInsert(p,1,10); heapInsert(p,2,2); heapInsert(p,3,7); heapInsert(p,4,9); heapInsert(p,5,20); heapInsert(p,6,5); treeTraversal(root); return 0; } GopiKrishna Komanduri Software engineer [EMAIL PROTECTED] --- On Fri, 28/11/08, Pedro Izecksohn [EMAIL PROTECTED] wrote: From: Pedro Izecksohn [EMAIL PROTECTED] Subject: Re: Res: [c-prog] Re: integer promotions To: c-prog@yahoogroups.com Date: Friday, 28 November, 2008, 6:20 AM --- Thomas Hruska wrote: Try compiling your code as C++ and see if there is a difference. C++ compilers tend to generate a lot more warnings as the language is, generally-speaking, more strict. [EMAIL PROTECTED] ~/programming/ c++/problem $ ls -la total 10 drwxr-xr-x+ 2 root None 4096 Nov 27 22:47 . drwxr-xr-x+ 11 root None 4096 Nov 27 22:35 .. -rw-r--r-- 1 root None 69 Nov 27 22:37 Makefile -rw-r--r-- 1 root None 261 Nov 27 22:46 problem.cpp [EMAIL PROTECTED] ~/programming/ c++/problem $ cat problem.cpp #include climits #include iostream using namespace std; int main (void) { unsigned short int a; unsigned long long int b, c; a = USHRT_MAX; b = (a*a); c = ((unsigned int)a*(unsigned int)a); cout Why hex b != c ?\n; return 0; } [EMAIL PROTECTED] ~/programming/ c++/problem $ cat Makefile problem : problem.cpp g++ -Wall -Wconversion problem.cpp -o problem [EMAIL PROTECTED] ~/programming/ c++/problem $ make g++ -Wall -Wconversion problem.cpp -o problem [EMAIL PROTECTED] ~/programming/ c++/problem $ ./problem.exe Why fffe0001 != fffe0001 ? Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/ [Non-text portions of this message have been removed]
Re: Res: [c-prog] Re: integer promotions
On Thu 2008-11-27 16:50:55 UTC-0800, Pedro Izecksohn ([EMAIL PROTECTED]) wrote: [EMAIL PROTECTED] ~/programming/c++/problem $ ls -la total 10 drwxr-xr-x+ 2 root None 4096 Nov 27 22:47 . drwxr-xr-x+ 11 root None 4096 Nov 27 22:35 .. -rw-r--r-- 1 root None 69 Nov 27 22:37 Makefile It's never a good idea to be running as root...