Matsen

Members
  • Content count

    142
  • Joined

  • Last visited

Everything posted by Matsen

  1. Unity template question

    Quote: The switch statement would still be needed upon construction of the array, but that would be it. I hope that makes more sense. Let me know! You're on the right track. I think you should separate the construction of the concrete file classes from the class itself. For example, you could create a separate function which creates a concrete file for you. The rest of your code will only deal with the base class, FileObjBase. FileObjBase* CreateFileObj(const std::string& fileName) { HANDLE fileHandle; // open the file and fetch the type switch (theFileType) { case TYPE_INT: return new FileObj<int>(fileHandle); case TYPE_FLOAT: return new FileObj<float>(fileHandle); case TYPE_MYTYPE: return new FileObjMyType(fileHandle); case TYPE_WEIRDTYPE: return new FileObjWeirdType(fileHandle); } return 0; // or throw an exception for unknown type } I can't see any reason for why your solution would not work, basically you're doing the same thing but with the array class. But, I think writing your own special array class when std::vector would do the work for you, with a different design, feels more appealing. Also, as your code shows, it looks like you're stuck with switch statements all over. By separating construction from the class, you will only need one switch statement and need only to create subclasses for any type you wish to support. Hope it helps
  2. Unity template question

    Why use a base class for the array class? Looking at your previous question, it seems as if a base class for the file class would be better: class FileObjBase { public: virtual void foo() = 0; virtual void bar() = 0; }; template <typename T> class FileObj : public FileObjBase { std::vector<T> array; public: void foo() {} void bar() {} }; Please elaborate a little on your design, if I've misunderstood you =)
  3. hello world

    You're mixing C++ and Managed C++ (.NET). Console::WriteLine is part of the .NET libraries while cin and cout is part of the standard C++ libraries. As Boder said, cin and cout are in the std namespace: std::cin, std::cout
  4. a design problem

    If it is correct to say that Material has a texture, then texture should be owned by Material. But, having a class where a texture is rarely used makes using the Material class a bit cumbersome, as you have to check if a texture is available. My suggestion would be to either create a sub class of Material which always have a texture, as "Material" is a very abstract concept. If this do not fit your design, you could always create some sort of a null-texture, so that the rest of the design can assume a texture always being present.
  5. Arrays with new operator

    std::string* layerOrder = new std::string[n]; ... delete [] layerOrder; Regards Mats Edit: I'm too slow
  6. Archiving files c++

    If compression is of no particular concern, the best might be to write your own simple compression routine. It's not very hard, search for Huffman or Adaptive Huffman encoding. Edit: Or RLE encoding. It depends on the data you wish to compress. Regards Mats.
  7. C++ help

    Sure there is. It seems as if game maker uses strings, but that seem unnecessary. The simplest way would be to use a class, with the different levels separated: case 1: player.hp += 30; player.mana += 3; case 2: player.hp += 32; player.mana += 4; . . etc If strings is what you want, you should take a look at std::string and std::stringstream. There are other ways, such as using masking for storing the levels in one 32 bit int (search for masking and bitwise operators). Your question is very general, so perhaps you should elaborate so we can give you a more precise answer. Regards Mats
  8. Multi Thread/Linked list problem

    Quote:Original post by Winegums are deleting the struct and deleting the thread two different things? if so, how do i delete each/both of them? i thought changing the struct instances 'running' variable to END would kill the thread, but it seems not. Yes. You have to delete the struct and let the thread return from doit(). The thread doesn't know about the struct and doesn't care either. It has a life of its own. Don't try to terminate the thread forcibly, that can cause havoc if you try to expand on this framwork of yours. As I said in my previous reply, changing the state to END probably does nothing, because the state variable is only read from memory once. Making it volatile might solve the problem, but this is a guess and even if it works, it's a very bad solution. Your solution will not allow for a 'theoretically limitless number of threads' because any computer will choke long before that. What you should do is use synchronization. An 'event' would be very useful in your case. Here's a symbolic example. The syntax is not correct. Replace the 'paused' state with an event. while (thread_properties->isAlive()) { thread_properties->event->wait(); // if paused, the thread is stopped here // do stuff } That will allow for as many threads and events as the OS can handle. As I also said, threads are tricky, so my suggestion is that you find some tutorials about threads AND synchronization before you continue. Edit: If this really is a school assignment or from an employer/co-worker you should run and never look back ;)
  9. Multi Thread/Linked list problem

    I don't understand what you're trying to do. Perhaps you should take a look at something called a thread pool, if that's what you want achieve. Anyway, first, you don't need the _endthread() call. It's called automatically when the thread returns from doit(). Second, don't use loops to prevent unused threads from dying. They will eat resources. Use some form of synchronization. Third, you're not deleting the thread entry, you simply assign the pointer a null value. Forth, this is a guess, but the reason why the thread doesn't die might be because the state variable is not read from memory each lap in the loop. Try making it volatile and see if that helps (volatile int state). Threads are tricky, so don't be discouraged! Regards Mats
  10. Or Suggestions on good *series* scifi novels!

    I recommend Peter F Hamilton's Night's Dawn Trilogy. Each book is about 1000+ pages, so you'll be busy for a while.
  11. operator overload sort problem

    operator< is not called because it's comparing pointers. Try this: bool less(const Obj* lhs, const Obj* rhs) { return lhs->getY() < rhs->getY(); } ... std::sort(drawObjects.begin(), drawObjects.end(), less); Regards Mats
  12. Access violation in dbgdel.cpp?

    You're probably deleting something twice, or maybe your trying to delete something that isn't dynamically allocated. Run a debug. Regards Mats
  13. The internet phenomenon (Consuming our lives)

    Quote:Original post by MindWipe But it's not really that easy. I think I want to study computer engineering, mostly because I've been coding for 10 years and it's been fun. BUT, I'm not so sure this is what will give me the most out of life, I was thinking becoming a teacher would really be much more rewarding. And maybe go somewhere, do something out of the blue, an impuslive action. It is easy. You can't foresee the future. You don't know what you want to do in 1, 5 or 10 years from now. So, just do what your heart/gut tells you to do and trust it. Put your head in a refrigerator for a while. Stop thinking and intellectualizing your situation. Worry about the future when the future is now. Until then, the future is an illusion that exists only in your head. You can't do something impulsive by trying to be impulsive. You have to stop thinking. Quote:People need to learn to be thankful for everything they take for granted. I agree 100% Regards Mats
  14. Quote:...but is it also safe to use class instances between threads? It depends on your class, its expected behaviour and design. And it's not inherently safe to share variables between threads either. It all depends :) Look at this: struct A { int some_variable = 0; void f() { if (some_variable == 0) { some_variable++; } } }; If two threads access a shared instance of A and call f at the same time, some_variable can accidentally be incremented twice. So, in the case above you'll need some form of synchronization. Basically, the only scenario where you don't need synchronization is when there are only read operations. As soon as you start adding some behaviour to the class, as in the case above, you'll probably need to serialize access. In your example, using a global variable to pause the demo/game seems a bit silly. Why not let all objects that can be paused have pause-methods? Regards Mats