Jump to content

  • Log In with Google      Sign In   
  • Create Account

TheComet

Member Since 02 Oct 2013
Offline Last Active May 20 2016 03:46 AM

#5241733 How is cross platform done

Posted by TheComet on 21 July 2015 - 11:20 AM

That's about as hard to answer as "how do people cook."

Most of it boils down to [...]

I see what you did there




#5238807 Multiple heritage on python

Posted by TheComet on 07 July 2015 - 10:21 AM

Still, it does not answer my question if super() is correct and how it works.

 

super() finds the next method in the MRO at each step. The proper way to call the base class __init__ methods is for every class to call super(), like this:
 
 

class A(object):
 def __init__(self):
  super(A, self).__init__()
  print('A init')


class B(object):
 def __init__(self):
  super(B, self).__init__()
  print('B init')


class C(A, B):
 def __init__(self):
  super(C, self).__init__()
  print('C init')


if __name__ == '__main__':
 c = C()

 
Perhaps this piece of code can shed some light:
 

class A(object):
 def foo(self):
  print('foo from A')


class B(object):
 def foo(self):
  print('foo from B')


class C(A, B):
 def foo(self):
  print('foo from C')

If we print out the MRO (method resolution order) of class C, we see the following order:
 

>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)

That's [C, A, B, object].

 

If we instantiate class C and call foo(), we get the expected output "foo from C":

>>> instance = C()
>>> instance.foo()
foo from C

super() can be used to call foo() from a base class. By specifying C as the class type, super() will return the next object in line in instance's MRO.

>>> instance.__class__.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
>>> super(C, instance).foo()
foo from A

Because instance is of type C and C was specified in super(), A is the next in line in [C, A, B, object].
 
By specifying A instead of C in super(), the next object after A in line in instance's MRO will be returned:

>>> super(A, instance).foo()
foo from B

Because instance is of type C and A was specified in super(), B is the next in line in [C, A, B, object].

 

By specifying B to super(), the next in line is object, which of course doesn't have the method foo():

>>> super(B, instance).foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'super' object has no attribute 'foo'



#5238677 Multiple heritage on python

Posted by TheComet on 06 July 2015 - 04:23 PM

This answers your question: http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance




#5237707 Unit setup

Posted by TheComet on 30 June 2015 - 11:55 AM

It's arbitrary. 1 meter can be 1 unit, but 1 meter could also be 45.7 units. It depends on how everything is scaled relative to everything else.




#5237335 Memory management patterns in C++

Posted by TheComet on 28 June 2015 - 03:50 PM

Have you tried to make a garbage collector or used reference counting?

 

Have you tried it?

 

"There will be a lot of bugs": Well yeah, but you can say that about anything. The arguments you're using have no sustenance. They are just opinions based on nothing.




#5232923 Copy file/folder cross-platform

Posted by TheComet on 05 June 2015 - 04:37 AM

You could do something as simple as:

 

#include <iostream>
#include <string>
#include <stdlib.h>


int copy_directory(std::string a, std::string b)
{
#if defined(_WIN32)
    return system(std::string("copy " + a + " " + b).c_str());
#elif defined(__APPLE__) || defined(__linux__)
    return system(std::string("cp -r " + a + " " + b).c_str());
#else
#   error copy_directory does not support this OS!
#endif
}   


int main()
{
    if(copy_directory("a", "b"))
        std::cout << "something went wrong" << std::endl;
    return 0;
}

 

If you want more control and don't want to use any external libs then you'll have to write multiple implementations using the filesystem API for each platform you want to support. In my project, I placed each implementation in a separate source file and had one common header file. Depending on the platform, the correct source file is compiled.




#5219814 Why didn't somebody tell me?

Posted by TheComet on 28 March 2015 - 08:28 AM

It's called the alphabet because the first two greek letters are "alpha" and "beta" --> "alpha" + "bet"




#5218686 c++ count lines in txt file and then read these lines without reopening a file

Posted by TheComet on 23 March 2015 - 11:21 PM

@WiredCat - This might be interesting to you: http://insanecoding.blogspot.ch/2011/11/how-to-read-in-file-in-c.html

Speed test of various file reading methods in C++.

 

 

 


and to people that said use vector no i wont because i dont want to recreate the array almost every line.

Vectors don't reallocate every line... They (usually) reallocate log(n) because they expand exponentially. If you read 5000 lines, the vector will reallocate ~12 times. Look, here's an example:

int main()
{
    std::vector<int> v;
    for(int i = 0; i != 5000; ++i)
        v.push_back(i);
    return 0;
}

Output:

$ valgrind ./test                       
==5628== HEAP SUMMARY:
==5628==   total heap usage: 14 allocs, 14 frees, 65,532 bytes allocated

See? 14 allocations, even though I pushed 5000 elements into it. That's not "every line".

 

Here's my fancy solution (C++11):

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>


class Line
{
    std::string data;
public:
    friend std::istream& operator>>(std::istream& is, Line& l)
    {
        std::getline(is, l.data);
        return is;
    }
    std::string operator() const { return data; }
};  


std::vector<std::string> get_file_lines(std::string file_name)
{
    std::ifstream in(file_name.c_str(), std::ios::in);
    return std::vector<std::string> {std::istream_iterator<Line>{in},
                                         std::istream_iterator<Line>{}};
}


int main()
{
    auto lines1 = get_file_lines("file.txt");
    return 0;
}

http://stackoverflow.com/questions/1567082/how-do-i-iterate-over-cin-line-by-line-in-c/1567703#1567703

http://stackoverflow.com/questions/236129/split-a-string-in-c




#5217690 Doge Indirection

Posted by TheComet on 19 March 2015 - 12:42 PM

Hackers have been around for too long, usually associated with dark themes and “the evil side” they represent the minority of IT Gurus that just have too much time on their hands, and a touch of evilness. Usually represented with a skull or “horrifically” they are iconic in a never ending battle against internet crime.
For this reason, I encourage all of us to start utilising the technique of indirection for storing all primitive datatypes in our programs. Not only will this make it impossible for hackers to track our variables, it will also allow us programmers to taste the sweet taste of having stopped said hackers with a doge meme.
#include <iostream>


template <class T, int N>
struct Indirection
{
    typedef typename Indirection<T, N-1>::type* type;
    typedef typename Indirection<T, N-1>::type  child_type;
    
    static void init(type& p)
    {
        p = new child_type;
        Indirection<T, N-1>::init(*p);
    }
    
    static void cleanup(type& p)
    {
        Indirection<T, N-1>::cleanup(*p);
        delete p;
    }
};


template <class T>
struct Indirection<T, 0>
{
    typedef T type;
    
    static void init(type& p) {}
    static void cleanup(type& p) {}
};


int main()
{

    /* there happen to be 272 stars in the doge below */
    Indirection<int, 272>::type cpu_cache_will_enjoy_this;
    Indirection<int, 272>::init(cpu_cache_will_enjoy_this);
    
    /*
     * set the value of our variable using the power of the shibe
     */


         *              *    
        * *           ** *     *         *     ****     *         *
        *  *        **   *     *         *   *      *   *         *
       ***  ********     *      *       *   *        *   *       *
     ***           *  ** *      *   *   *   *        *   *   *   *
   **               **** *       * * * *     *      *     * * * *
  *   **              **  *       *   *        ****        *   *
  *  ***     ****       * *  
 *           ****        *** 
 *  ****                   * 
* ******** *               * 
*  ****  ** *               *          **              *
*   ****   *               *          *                *
 *      ***                *           **   *  *   **  * **
 *                     *  *              *  *  *  *    **  *
  **                 *    *            **    **    **  *   *
    **          ****    **   
      **********     ***                     *            *  *                *   *
                   **                                     *                  ***
                                             *  * **   ** *  *  ***  **   **  *   *   **   * **
                                             *  *   * *  **  *  *   * ** *    *   *  *  *  *   *
                                             *  *   *  ** *  *  *    **   **   *  *   **   *   *


    cpu_cache_will_enjoy_this = 20;
    
    /*
     * now output the value to stdout
     */
    
    std::cout << " the value is: " << ****************************************** // make absolutely sure not to go over the 80 character per line limit, as to not enrage The Powers That Be (Linus Torvalds) in the mailing list
    ****************************************************************************
    ****************************************************************************
    ********************* ******************************************************
    ***cpu_cache_will_enjoy_this << std::endl;
    
    /*
     * clean up like good programmers - no memory leaks, check it with valgraind
     * if you don't believe
     */
    
    Indirection<int, 272>::cleanup(cpu_cache_will_enjoy_this);
    
    return 0;
}
Granted, the error messages can become a little bit confusing, but all you have to do is write a batch script to parse the error output in order to understand how many stars you forgot:
 
/home/thecomet/documents/programming/test/main.cpp: In function ‘int main()’:
/home/thecomet/documents/programming/test/main.cpp:81:61: error: no matching function for call to ‘Indirection<int, 273>::cleanup(Indirection<int, 0>::type********************************************************************************************************************************************************************************************************************************************************************************&)’
     Indirection<int, 273>::cleanup(cpu_cache_will_enjoy_this);
                                                             ^
/home/thecomet/documents/programming/test/main.cpp:81:61: note: candidate is:
/home/thecomet/documents/programming/test/main.cpp:15:17: note: static void Indirection<T, N>::cleanup(typename Indirection<T, (N - 1)>::type*&) [with T = int; int N = 273; Indirection<T, N>::type = int*********************************************************************************************************************************************************************************************************************************************************************************; typename Indirection<T, (N - 1)>::type = int********************************************************************************************************************************************************************************************************************************************************************************]
     static void cleanup(type& p)
                 ^
/home/thecomet/documents/programming/test/main.cpp:15:17: note:   no known conversion for argument 1 from ‘Indirection<int, 272>::type {aka int********************************************************************************************************************************************************************************************************************************************************************************}’ to ‘Indirection<int, 0>::type*********************************************************************************************************************************************************************************************************************************************************************************& {aka int*********************************************************************************************************************************************************************************************************************************************************************************&}’
CMakeFiles/indirection.dir/build.make:54: recipe for target 'CMakeFiles/indirection.dir/main.cpp.o' failed



#5215724 Best programming paradigm for noobs?

Posted by TheComet on 10 March 2015 - 04:53 PM

If a noob, who knows nothing about computer programming, wishes to embark on the painful journey of developing software, what is the best paradigm to start with and why? Procedural? OO? Functional?
 
 
I've been thinking about this lately because at college Java is the first thing to be taught and I noticed there to be a lot of confusion among the students. Those who have never been close to computer code are having an extremely hard time grasping the concepts. Those who have programmed before are having less of a hard time, but are still utterly confused (try explaining to them what the "this" keyword does, for instance).
 
I personally started out with BASIC when I was 14, and much much later I decided to learn about OO. I think it's a lot easier to learn a procedural language before learning OO.
 
What do you think?




#5211085 What are the recommended places to store save data?

Posted by TheComet on 16 February 2015 - 06:21 PM


Call me old-school but whatever happened to storing system configuration in /etc?

/etc is for system configurations, user application configurations go into /usr/local/share or /usr/share if they are not to be touched by the user.




#5211060 What are the recommended places to store save data?

Posted by TheComet on 16 February 2015 - 04:06 PM

On Windows machines it's best to place savefiles in %appdata%. Depending on the situation you can also place them directly in the home folder for them to be more accessible to the end user.

 

On Linux there are a few locations, most applications put user created content in ~/.local/share/<app_name>/ and user configuration files in ~/.config/<app_name>/. System configurations (things the user shouldn't touch) are usually placed in /usr/local/share/<app_name>/. Again, depending on the situation, you may also want to place user files directly in the home folder ~/.

 

I am not sure what mac does.




#5207578 Some programmers actually hate OOP languages? WHAT?!

Posted by TheComet on 29 January 2015 - 06:48 PM


Still... what might "well-coded" games in C look like? Or would it be quite close to a game in C++ but mostly just without the encapsulation given by things like access modifiers, and semi-advanced concepts like templates?

Being someone who is currently coding a game in C for the lulz, I think I can answer this.

 

The structure of code is quite different than what I'd do in C++, but the same basic concepts apply. For instance, a lot of systems have system_init() and system_deinit() functions instead of constructors/destructors. There are no classes with methods, but there are structs for holding data and functions which act upon the data of these structs. Templates don't exist, but you can implement the majority of what templates are used for with macros. Compositing can be done with structs.

 

Polymorphism doesn't exist per se. I've seen my fair share of home made polymorphism in C, but I believe it's not really necessary. Most of the time you can composite. 




#5207543 Programming

Posted by TheComet on 29 January 2015 - 04:20 PM

Spiro speaks the truth.

 

I started a project as a somewhat average programmer with some artists, but I chose a game that was so far out of my league we had to can the entire thing after a few months. I disappointed everyone on the team.

 

One of the team members wrote me a really long and hurtful message. Spraying salt on my skinned body would have been less painful than some of the things he said, but it was the truth. He felt extremely passionate about what we were creating, and he blamed me for being an incompetent idiot for throwing the entire thing away and wasting everyone's time.

 

Trust me, you do not want to go through that pain. I learned my lesson the hard way.




#5195162 Best comment ever

Posted by TheComet on 28 November 2014 - 06:43 AM

These are hilarious. During my undergrad I was working on a partner project and found this in a 20 line .c file:

;;;;;  ;;;;;;  ;;      ;; ;;             ;;;;;;;  ;;;;;;;  ;;       ;;;;;;;  ;;   ;;  ;;;;; 
;;     ;;      ;;;;  ;;;;                ;;       ;;   ;;  ;;       ;;   ;;  ;;;  ;;  ;;   
;;;;;  ;;;;;;  ;;  ;;  ;; ;;   ;;;;;;    ;;       ;;   ;;  ;;       ;;   ;;  ;; ; ;;  ;;;;;
   ;;  ;;      ;;      ;; ;;             ;;       ;;   ;;  ;;       ;;   ;;  ;;  ;;;     ;;
;;;;;  ;;;;;;  ;;      ;; ;;             ;;;;;;;  ;;;;;;;  ;;;;;;;  ;;;;;;;  ;;   ;;  ;;;;;

Which much to my surprise compiles just fine.

(;) is a null statement and usually compiles to nothing at all.

 

A compiler I was working with for programming MSP430 microcontrollers would interpret a null statement as an actual NOP instruction. If I had pasted that thing into our code a lot of people would have been mad.






PARTNERS