Thread: Not so much of a technical question...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Not so much of a technical question...

    Let me start out by saying this: I love C

    Now, with that out of the way.. I want to create a simple game, and that really cries OOP so C++ would be a much wiser choice. The problem is that I hate <iostream>, <string>, etc. I like char *buffer much better. Now the big question is... is it ok to take the parts of C that I like (read(), write(), etc...) and use them in C++? I know it will work and produce the desired results but is it good coding practice? Or maybe I should just suck it up and learn the C++ way of doing things?

    Thanks,

    Sam

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by swinchen
    The problem is that I hate <iostream>, <string>, etc.
    No, I'd say the problem is having an emotional reaction to standard libraries.

    I'd suggest learn more. Just one more tool in the toolbox.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends on what you mean by good coding practice. If you are trying to learn to use C++ to its fullest potential, then using all the C ways of doing things is not good practice. If you are merely trying to augment your C code with some of the features available in C++, then IMO that's fine.

    The benefits of sucking it up and learning the C++ way include learning new programming paradigms that aren't available or are clumsy in C. In addition to OOP, generic programming is a useful tool and practice with that tool would make good experience. Of course, if your goal is to make the best game you can make as opposed to learning about programming, then that benefit isn't as important. However, if you do want to continue programming in C++ in the future, it will get you into some bad habits if you start out with a C frame of mind.

    One suggestion if you do decide to stick to C code in C++ - be aware of functions like malloc or memcpy that don't pay attention to constructors or objects. For example, if you use malloc to allocate space for an array of class objects, the constructors for those objects won't get called. You have to use new. When mixing C code with C++ classes you have to look out for those problems.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The only thing I have to add to Daved's comments is that I very highly recommend using new/new[] and delete/delete[] in C++. Using malloc et al, I would consider bad programming practice.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why is it bad programming practice? Is new any better than malloc()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Why is it bad programming practice? Is new any better than malloc()?
    Quote Originally Posted by Daved
    One suggestion if you do decide to stick to C code in C++ - be aware of functions like malloc or memcpy that don't pay attention to constructors or objects. For example, if you use malloc to allocate space for an array of class objects, the constructors for those objects won't get called. You have to use new. When mixing C code with C++ classes you have to look out for those problems.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Thanks for the replies.

    It gets very confusing for me when I am looking through tutorials written in C++ that have classes but use #include <stdio.h> and then use fread, fwrite... etc. Even OpenGL Game Programming by Hawkins and Astle seems guilty of this.

    To tell you the truth I have a hard time telling where C ends and C++ begins. A lof of the times I am unsure which one I am using. I know that <stdio.h> is the C Standard IO include file.

    I will try my best to stick with "straight" C++

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Quote Originally Posted by http://www.codeguru.com/forum/showthread.php?t=344569
    Q: What about the traditional ANSI C standard header files?

    A: The traditional ANSI C standard header files are prefixed with the letter 'c'. Thus 'stdio.h' has become 'cstdio', 'stdlib.h' is called 'cstdlib', 'math.h' is called 'cmath', 'time.h' is called 'ctime' and so on.
    However, I guess when using C++ you should stick strictly to the iostream, and not use stdio type things.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    So what happens when I include stdio.h? hrmmm....

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Usually, the compiler has it (usually the compiler is for C or C++), and so, it includes the files, links the libraries, and goes merrily on its way, doing what you want. (Note that the traditional C headers do not declare functions/structures in the std namespace.) But, <stdio.h> is part of the C standard library, and <cstdio> is part of the C++ standard library.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    So at the risk of beating a dead horse,

    There should never be a reason to include something like stdio.h in a C++ project because cstdio (if for some reason you need to use C style IO) which is made specificaly C++. If you use stdio.h, it works because C++ is compatible with C.

    Hmmm this could be a clearer.

    Then there are things like <unistd.h> in that case <cunistd> doesnt exist, so you have to use the C version.

    Do I have this about right?

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> There should never be a reason to include something like stdio.h in a C++ project because cstdio (if for some reason you need to use C style IO) which is made specificaly C++. If you use stdio.h, it works because C++ is compatible with C. <<
    Correct. One is in the C standard library, and the other in the C++ standard library.

    >> Then there are things like <unistd.h> in that case <cunistd> doesnt exist, so you have to use the C version. <<
    That is a non-standard header, so just know that any program that uses it will not necessarily be portable to other compilers or platforms (in this case, most likely there will only be cross-platform issues if you use relatively mainstream compilers).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Technical Specialist - Update Delivery - C Required
    By ISCRecruiter in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 07-21-2008, 09:50 AM
  3. callbacks and threads - technical question
    By Jumper in forum Windows Programming
    Replies: 2
    Last Post: 07-11-2004, 12:09 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM