Thread: pls post here tricks that you know of in C/C++, thanks

  1. #31
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i thought you were more than an amature...

    or maybe you're just a stuck up SOB that is too good to realize that everybody has to start somewhere, and that would be considered a trick by somebody that's just starting out...


    no, i'm not trying to start a flame war...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #32
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    To fix MSVC6's problem with the scope of variables in a for statement:
    Code:
    #define for if( 0 ); else for
    And to deal with its problems with the std namespace and C++-style headers:
    Code:
    #ifdef _MSC_VER
    #if _MSC_VER < 1300
    namespace std {
    #endif
    #endif
        #include <cstdio>
        // and whatever else
    #ifdef _MSC_VER
    #if _MSC_VER < 1300
    }
    #endif
    #endif
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #33
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I´m not the most experienced programmer, but I´ll try to leave a good one.
    When using static variables in a class(or any other place), remember to put a reference to that variable in the definition file, for example:
    Code:
    /* file.h */
    class T{
       public:
          T();
       private:
          static int tX;
    };
    
    /* file.cpp */
    #include "file.h"
    
    int T::tX;
    
    T::T(){
    
       tX = 10;
    }
    Nothing more to tell about me...
    Happy day =)

  4. #34
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A couple I can think of off the top of my head:

    Using single quoted characters to initialize integers. For example:

    'a' is 8 bits
    'aa' = ('a' << 8) + 'a' is 16 bits
    etc...

    To see if your OS protects memory well, run the following program:

    Code:
    int main()
    {
        for(int* p = (int*)0x1000; ; *(++p) = 0);
    }
    If it does, the program crashes. If it doesn't, the system crashes.
    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. #35
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    heres one. why wait until runtime to compute a square root if it can be done at compile time.
    Code:
    template<int N,int LO = 1,int HI = N>
    class Sqrt
    {
       public:
          enum { mid = (LO + HI + 1) / 2};
          enum { result = ( N < mid * mid) ? Sqrt<N,LO,mid - 1>::result : Sqrt<N,mid,HI>::result};
    };
    
    template<int N,int M>
    class Sqrt<N,M,M>
    {
       public:
          enum{ result = M};
    };
    
    #include<iostream>
    
    int main()
    {
       std::cout<<"square root of 49 is "<<Sqrt<49>::result<<std::endl;
       return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #36
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Don't forget Fibonacci's

    Code:
    template<int N>
    struct fibonacci
    {
        enum { ret = fibonacci<N - 1>::ret + fibonacci<N - 2>::ret };
    };
    
    template<>
    struct fibonacci<0>
    {
        enum { ret = 1 };
    };
    
    template<>
    struct fibonacci<1>
    {
        enum { ret = 1 };
    };
    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.

  7. #37
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    Guys!!!! wait up!!! Please!!!

    not here, on my website! please post your tips and tricks there together with your name, and optional email ad and homepage!

    tips and tricks collection

    please? thanks a lot!!!
    http://www.angrysoft.com/
    Get SMS Counter and Call Counter for Nokia 7650/3650

    http://www.angrysoft.com/tipsandtricks.php
    Please contribute some tips and tricks that you know of.

  8. #38
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by major_small
    i thought you were more than an amature...

    or maybe you're just a stuck up SOB that is too good to realize that everybody has to start somewhere, and that would be considered a trick by somebody that's just starting out...


    no, i'm not trying to start a flame war...
    I was just implying that not even an amatuer would find most of those tricks terribly amazing.

    You're mean; I'm telling my mommy on you.

    *sniff*

  9. #39
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    sorry... i was just pointing out that you shouldn't overlook the obvious...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #40
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Well, at least you apologized.

    Ok, I won't tell my mommy on you.

  11. #41
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Can anyone explain the above sqrt / fib code? I don't understand the role of the enum....

  12. #42
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Following on from Stoned_Coder's metaprogramming example...here's something I was working on for a contest a while ago...it was to add 2 fractions at compile time and rationalize them down....bit of a pig to read!

    Code:
    #include <iostream>
    #include <cassert>
    //Main struct interface
    template <int n1, int d1, int n2, int d2>
    struct add_fraction
    {
       static const int numerator;
       static const int denominator;
    };
    //Specialisation 1: Subvert functionality
    template <int n1, int d1>
    struct add_fraction<n1,d1,0,0>
    {
       static const int denominator = add_fraction<d1,n1%d1,0,0>::denominator;
    };
    //Specialisation 2: Assign value
    template <int n1>
    struct add_fraction<n1,0,0,0>
    {
       static const int denominator = n1;
    };
    //The Numerator
    template <int n1, int d1, int n2, int d2>
    const int add_fraction<n1,d1,n2,d2>::numerator = 
    ((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2)) /
        add_fraction<((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2)),
        ((d2*d1)%((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2))),0,0>::denominator;
    //The Denominator
    template <int n1, int d1, int n2, int d2>
        const int add_fraction<n1,d1,n2,d2>::denominator = (d1*d2)/
        add_fraction<((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2)),
        ((d2*d1)%((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2))),0,0>::denominator;
    
    
    int main() {
    	typedef add_fraction<2,3,1,12> three_fourths;
    	assert(three_fourths::numerator==3);
    	assert(three_fourths::denominator==4);
    
    	std::cout << three_fourths::numerator;
    	std::cout << std::endl << "--" << std::endl;
    	std::cout << three_fourths::denominator;
    }

  13. #43
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Can anyone explain the above sqrt / fib code? I don't understand the role of the enum....
    To explain this lets look at the simpler fibonacci example. Lets refresh ourselves of the code...
    Code:
    template<int N>
    struct fibonacci
    {
        enum { ret = fibonacci<N - 1>::ret + fibonacci<N - 2>::ret };
    };
    
    template<>
    struct fibonacci<0>
    {
        enum { ret = 1 };
    };
    
    template<>
    struct fibonacci<1>
    {
        enum { ret = 1 };
    };
    That is 1 template struct and 2 full specializations. The enum held is a compile time constant. It could have equally have been a static const int as in fordy's example.

    lets look at fibonacci<0>::ret its 1
    fibonacci<1>::ret is also 1
    fibonacci<2>::ret however expands to this....
    ret = fibonacci<1>::ret + fibonacci<0>::ret = 1+1 = 2
    fibonacci<3>::ret expands to
    fibonacci<2>::ret + fibonacci<1>::ret = 2 + 1 = 3
    fibonacci<4>::ret expands to
    fibonacci<3>::ret + fibonacci<2>::ret = 3 + 2 = 5

    As you can see this is all worked out at compile time using recursive template techniques. Now you can see how the fibonacci example works try to work thru the square root example to see how that works.Try tracking the template instantiations for a simple square root like the square root of 16
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #44
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    int add( int a, int b ) {
        int d;   
        (&d)[ ( ( 0x188 - 0304 - 0xA1 + 154 ) / 025 ) - ( 0xA098 / 010730 )]  = (0x32 - 062)[&a] + (0x21 - 041)[&b];
        return d;
    }
    [edit]Added return statement
    Last edited by XSquared; 06-09-2003 at 09:25 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  15. #45
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Ahh templates.. So elegant in theory, so hideous in implementation.

    XSquared... I think there may be a bug in your code... For me it only 'returns' a.
    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. Beginning Game Programming Type Books
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 09-13-2006, 04:15 PM
  2. C programming - pls pls help me
    By sally arnold in forum C Programming
    Replies: 10
    Last Post: 01-16-2002, 04:55 AM
  3. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM
  4. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM
  5. 40 post drop
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-12-2001, 12:14 PM