|
From: Adam J. <aj...@i-...> - 2007-08-24 14:07:47
|
There are some other items that I would be concerned with in your code. Maybe because this is a test is why you did what you did. Here are some comments about what you are trying to do. I believe the explanation you got from the others explain what is happening. It also appears that You are trying to mix two styles (C style programming with C++ Style programming). My advice is to pick one. You mix C headers with C++ header files, some of which overlap functionality. > #include <cstdlib> <== This is a C++ implementation of the C stdlib > #include <iostream> <== This is a C++ io implementation header > #include <stdio.h> <== This is a C io implementation header > #include <conio.h> <== This is a C Style console io implementation file that is not support by all compilers on different OSes As you can see you have three different io implementations, and what makes it worse is that you only use the functionality in iostream. The other io headers aren't used in your code. My suggestion is only include the header files you need. > pa = (bartek*) x; The cast listed above is a C Style cast not a C++ cast (Mixing Styles...). C++ uses static_cast, dynamic_cast, etc. Be careful when you do cast. You can get yourself in a lot of trouble. Just because the compiler doesn't complain, doesn't mean your program will run correctly. You are mixing two types that don't really go together nicely. Your user defined abstract data type (bartek) and the native int type. > Per Wrote: > Never play with pointers. Only convert between compatible pointers. He is exactly right. I would expand on this by saying types instead of pointers. But the idea is the same. As you found out this utimately was part of your problem. The assumetions you made about how the casting would be implemented. One final point, if you are going to ceate your own types and manupilate them, don't try and reinvent the wheel. C++ has the STL, part of the STL is containers and iterators. If you create your abstract types to conform to the STL, it will make your life a lot easier. You should use the STL containers, iterators, and algorithms as much as you can. They are time tested and very efficient at what they do. -Adam Jones -i-softwareproducts.com |