Jump to content

  • Log In with Google      Sign In   
  • Create Account


Khatharr

Member Since 24 Apr 2010
Offline Last Active Today, 02:57 AM

#5104508 Need Code Translation From Pascal/Delph to C++

Posted by Khatharr on 25 October 2013 - 07:49 PM

Is There any shorter Coding Than  strcpy(customer.name,"Jeremy Smith");

                                                        strcpy(customer.cell,"123456789");

 

-.-

If you want it simpler than that then use a std::string.

Also, when dealing with C-style strings, at least use a safe strcpy function so you don't overflow.

 

I can intialize everything at time of instance, For the short Program than I am writing ( Testing some theories in a short test program )  in which depending upon user input, I would change these entries from 1 set of data to another then maybe back to the first.  All may not change, maybe just customer.cell ?

 

Has anyone really been far even as decided to use even go want to do look more like?




#5104116 How can I gain a deeper understanding of C/C++?

Posted by Khatharr on 24 October 2013 - 09:16 AM

Some of the advices might be true here. But if you really want to understand how programming and computers really work together, you should learn Assembly language, at first you might be scared of it, because a lot people think it's the holy grail of programming languages, and they think that only best of the best can learn it.

 

Ah, yep. Saw the title, read the post, scrolled down and someone beat me to it.

 

If you want a deep understanding of what's going on under the hood, then take the hood off. Learn assembly and start using the disassembly view in your debugger to see what your compiler does in order to implement things. It's fun, it's hands-on, and it will change the way you look at the code you write.




#5103373 Will it be C++ the preferred game dev language in 3 years from now?

Posted by Khatharr on 22 October 2013 - 07:23 AM

I'd like to see a complete rehash of C++ into a new language, honestly. I appreciate the work that's being done by the committee, but making a clean break and starting over would give us an opportunity to ditch a lot of the old baggage and get some much nicer means without sacrificing the ends.




#5102971 Practical help on making a simple bullet hell

Posted by Khatharr on 20 October 2013 - 05:35 PM

You'll probably want to make a 'schedule' file for each stage, which the program will load and then proceed through in order to play the stage events at their correct times. For instance, a stage file can start with information about what music to play, what the background is, what enemies are present (so you can start caching resources), etc. You load that information from the file, then start a timer. The rest of the file will have simple commands (at the 30 second mark, spawn enemy type E at position X,Y - or things like that). Make sure those are all in chronological order, then as the stage plays it can just check to see if it's reached the time of the next command, and if so it executes it and moves to the next one.

 

As for 'controlling' bullets and such, that's usually simple deterministic AI. Just make an abstract Bullet class and then derive each bullet type from that. give each bullet type its own logic, and update each of them every frame.




#5102311 The Copy&Swap Idiom

Posted by Khatharr on 17 October 2013 - 11:03 PM

Is this not how it's done traditionally? Or am I missing something?


'traditional' isn't a technical term, but in this context it would mean 'plainly wrong and rather straw-mannish'. Of course you can get leakage if you do it that way. You're using raw pointers for resource control. That doesn't mean that C&S is the only alternative. Doing it right is another alternative, as I showed in the block after that.

C&S collects the copying behavior and then adopts it in a single transaction. That's the correct pattern, but C&S is not the only way to implement that pattern, and it can be unclear, make the machine do more work than it needs to, and/or trigger side-effects.

Using smart pointers or other RAII implementors allows you to prepare the new state, and then once everything is created successfully you adopt it. That's the 'normal' way of writing exception safe code. C&S is just a trick for doing that by invoking the copy ctor and then adopting it only if construction succeeds.

 

It does not magically guarantee exception safety.
 

Your code here is NOT exception safe!

    // copy constructor
    Player( const Player& that ) : m_Texture( new Texture(*that.m_Texture) ),
                                                  m_Sprite( new Sprite(*that.m_Texture) ),
                                                  m_Health( new m_HealthController(*that.m_Health) )

    {
    }

What if the allocation for m_Health fails? m_Texture and m_Sprite have already been allocated! How will you reach them in order to release them, since the object failed construction?




#5102087 The Copy&Swap Idiom

Posted by Khatharr on 17 October 2013 - 03:54 AM

 


I get the idea of copy and swap, but you're really just offloading the work to the copy ctor, aren't you? You could just as easily write the code into the assignment operator and then write the copy ctor to just call the assignment operator. If you're copying resources then you have to write the explicit copy behaviors somewhere.
  • The Big Rule Of 3 states: If you need either a copy-constructor, destructor, or overload the assignment operator, you will need all 3. http://en.wikipedia.org/wiki/Rule_of_three_%28C++_programming%29 This is always the case if your class manually manages resources (e.g. uses 'new' for its member variables).
    It is good practice to have a swap method for such classes in addition to all of that (the "3 and a half rule", some people call it). So with all of this in place, it is usually easier to write a copy&swap implementation and offloading the workload to the copy constructor rather than duplicating your copying code.
  • A copy-constructor's initialisation list will execute noticably faster than manually assigning values. http://stackoverflow.com/questions/13894415/c-creating-objects-initialization-lists-vs-assignment
  • When using copy & swap, no needless self-assignment checks have to be performed ( if( &cp == this ) return *this; )
  • copy & swap is guaranteed to be exception safe.

The bottom line is: There are more pro's than con's for a copy&swap over the traditional method.

 

-.-

 

Look, if you're talking Ro3 then you have at least one member that won't copy correctly on its own. That means that you're going to be specifying explicit behavior somewhere in there, otherwise there's no reason to write the functions. If that behavior is not exception safe, regardless of which method you put it in, then you don't have exception safe code. If it is exception safe then you do have exception safe code. C&S will not change this. When you accept by value in operator=(), you are triggering the copy ctor. If the copy ctor is not safe then you can have leaks.

 

Initializers can be faster than manual assignment, but you need to bench this before asserting that you're saving the universe through premature optimization here. believe it or not, a comparison for equality is likely to be a little more lightweight than constructing a whole new object in a significant number of scenarios. Additionally, the reason initializers are faster than manual assignments is that manual assignments in a ctor are touching members that have already been instantiated. You're not instantiating new members in operator=(), since they already exist, so manual assignment is not slower.




#5100721 Coud any one explain me the following code "self"

Posted by Khatharr on 11 October 2013 - 11:54 PM

A singleton named manager...

 

Sounds like it should be renamed 'Monotheos'.




#5100386 Unit Testing ftw?

Posted by Khatharr on 10 October 2013 - 07:26 PM

This video killed all of my interest in automated unit testing.




#5100079 Should i use a linked list for this (C++)?

Posted by Khatharr on 09 October 2013 - 10:58 PM

I'm sorry, isn't the correct solution here to use proper memory management?




#5099259 C++ Pointer clean up question

Posted by Khatharr on 06 October 2013 - 10:04 PM

You need to very clearly separate the idea of a pointer from the idea of an allocation. A pointer, no matter what you do to it, or with it, is variable that holds a memory address.

 

Allocations are created and need to be destroyed later to avoid leaks.

 

Their only relation is that the value in a pointer can point to an allocation.

 

It sounds simple, but getting really, really clear on that will solve all your pointer problems. Never let yourself think of a pointer as the thing it points to. Force yourself to think in terms of referencing and dereferencing. When that becomes second nature, you'll be able to intuit processes much more easily.

 

Also, in that example code, it's going to continue iterating the pointers in forwardlist, so DoSomething() will only happen to the last pointer found. You may want to throw a break in there after the assignment.




#5097544 WT* ?

Posted by Khatharr on 28 September 2013 - 09:42 PM

Wow, Cornstalks. That was impressive. You managed to figure out what the code actually said (`800 / eng->mWidth'), even though the OP posted something else.

To eltharynd: Please copy and paste code in the future so we don't have to rely on Cornstalks's crystal ball.

 

Once again I'm impressed by Cornstalks' balls.




#5097386 potatoeses or potatoeseses

Posted by Khatharr on 27 September 2013 - 10:18 PM

 

You could also just use a vector.

 

I was going to suggest that, but I've only used vectors without args so far and I was unclear about whether or not you have to use the second arg when using the first. Cool beans. That's probably the best solution.

 

 

 

Double indirection is going to annihilate your cache efficiency...


Please do tell.

 

 

The cache likes it when you access things that are in order or near to one another in memory. When you use indirection you're accessing things all over the place, and maximizing your chances of cache-misses. Using contiguous storage, like a vector, or a flyweight (or both) will make it a lot more likely that the next element you want is in the cache and ready to go.




#5097370 potatoeses or potatoeseses

Posted by Khatharr on 27 September 2013 - 08:44 PM

Double indirection is going to annihilate your cache efficiency...




#5097364 Gamemaker,RPGmaker, etc.

Posted by Khatharr on 27 September 2013 - 08:26 PM

There is that Engima Engine/SDK/something one of the users have been promoting. Supposedly is RPG Maker compatible and free.

 

What is this? I don't see it on google.

 

Edit - Never mind. I found it. Game Maker compatible, you mean.




#5097363 potatoeses or potatoeseses

Posted by Khatharr on 27 September 2013 - 08:24 PM

const size_t BUFFER_LENGTH = MAX_OBJ_COUNT * sizeof(Class);

//allocate a buffer and cast it
char buffer[BUFFER_LENGTH];
Class* objects = (Class*)buffer;

//use placement new
new (objects[0])Class(); //create a Class object at memory location objects[0]
new (objects[1])Class(); //etc - don't call delete on these when you're done with them. Just call their dtors manually.

Better yet, just design the class to have an active/inactive flag and use an object array. Have the class ctor turn the active flag off and do no other work. Set a pointer to the start of the array. When you add an object, activate the object at the pointer position and configure it, or just copy/move it from a configured outside object. Advance the pointer object-by-object until it points to an inactive position, or to the position past the end (indicating that the array is full).

 

Overhead, I am implementing a high-performance library which will be used for extreme tasks and those tasks greatly increase in resource consumption dependant on countless calls to the library. Worse case scenario I would use the pointers, but I hope someone knows of a better solution.

 

 

Sounds like a questionable design, but that's really not much information to go on.

 

Another option is the flyweight pattern.






PARTNERS