For all you C++ coders out thar

Talk about random stuff that has nothing to do with TF2 or other games.

For all you C++ coders out thar

by Vivi Hill » Thu Nov 04, 2010 12:20 am

During one of his first programming labs, a worried student presented his code to the TA, asking "Why is my counter not working?"

Code: Select all
class CountMachine {
   public:
       CountMachine() {
           int counter = 2;           
       } 
       void CountUp() {
           counter++;
       }
};

To which the TA replied, "I'm afraid that's out of the scope of this class."


Also, issues with steam thingie.
Image
"Just because nobody complains doesn't mean all parachutes are perfect." --Benny Hill
User avatar
Vivi Hill
Red Admin
Master of Ceremonies
 
Posts: 1498
Joined: Sat Oct 09, 2010 12:27 pm
Location: Troy, NY

Re: For all you C++ coders out thar

by Argent » Thu Nov 04, 2010 3:11 am

Knowing only intro to VB.Net, I can sense a spectacular pun in there, but not knowing C++ I don't think I get it.
Image

Image

>[I] TAKE THAT, CLOCKS
User avatar
Argent
Donator
The Fourth Suited Ace
 
Posts: 486
Joined: Fri Oct 15, 2010 12:23 pm

Re: For all you C++ coders out thar

by Divide by NaN » Thu Nov 04, 2010 6:19 am

Cute.
Image
User avatar
Divide by NaN
 
Posts: 103
Joined: Tue Oct 12, 2010 7:55 pm

Re: For all you C++ coders out thar

by Plasma Waffle » Thu Nov 04, 2010 1:05 pm

LOL
User avatar
Plasma Waffle
 
Posts: 80
Joined: Wed Oct 20, 2010 9:36 pm

Re: For all you C++ coders out thar

by dieinafire » Thu Nov 04, 2010 1:42 pm

baddabing!
User avatar
dieinafire
Donator
 
Posts: 63
Joined: Wed Oct 13, 2010 10:05 pm

Re: For all you C++ coders out thar

by rawkies » Thu Nov 04, 2010 8:29 pm

ba dum.







tshhhhhhhhhhhhh
Image
rawkies [r/trees]: nigga gimme 10 bitches and ill fuck all 10 --Snoop Doggy Dogg
セクシーのきたないお: words to live by
User avatar
rawkies
 
Posts: 256
Joined: Mon Oct 18, 2010 2:03 am
Location: San Leandro, CA

Re: For all you C++ coders out thar

by Echoplex » Fri Nov 05, 2010 12:03 am

Could someone make the point of this joke public please :|
Image

All stock errythang
User avatar
Echoplex
 
Posts: 676
Joined: Mon Oct 11, 2010 2:14 am
Location: Madison, WI

Re: For all you C++ coders out thar

by Vivi Hill » Fri Nov 05, 2010 12:17 am

Big Bear wrote:Could someone make the point of this joke public please :|

C and C++ have a concept called "scope". If a variable is declared inside curly braces ( { } ), it's deleted as soon as the code reaches the matching right brace. The snippet of code here defines a class, which is a method of storing and manipulating data. In any case, the variable "counter" is declared between the two brackets inside the CountMachine() class constructor (in the "int counter" declaration). Since it's deleted as soon as it hits the "}", it is no longer available outside that constructor. In coding the CountUp() member function, which presumably might be called after construction of a new CountMachine, the programmer is trying to access the "counter" variable declared in the constructor, but, since it's out of scope, the code is invalid.

Thus, the TA is both painting an accurate description of the code and the situation he is in in explaining the reasoning behind the problem in the code. Granted, if learning about scope is out of the scope of the programming course, it's definitely not advanced enough to involve objects.

For the interested, the following would be a correct (if sparse) implementation of the CountMachine class. Since counter is now a member variable, it stays in scope throughout the class and is usable in any functions declared in CountMachine.

Code: Select all
class CountMachine {
   public:
       CountMachine() {
           counter = 2;           
       }
       void CountUp() {
           counter++;
       }
   private:
       int counter;
};
Image
"Just because nobody complains doesn't mean all parachutes are perfect." --Benny Hill
User avatar
Vivi Hill
Red Admin
Master of Ceremonies
 
Posts: 1498
Joined: Sat Oct 09, 2010 12:27 pm
Location: Troy, NY

Re: For all you C++ coders out thar

by Echoplex » Fri Nov 05, 2010 12:28 am

I know, that's why I used to term public :P
Anyway, I've done a couple semesters of Java but I stopped at data structures :|
Image

All stock errythang
User avatar
Echoplex
 
Posts: 676
Joined: Mon Oct 11, 2010 2:14 am
Location: Madison, WI

Re: For all you C++ coders out thar

by rawkies » Fri Nov 05, 2010 12:44 am

Big Bear wrote:I know, that's why I used to term public :P
Anyway, I've done a couple semesters of Java but I stopped at data structures :|

wat.

If you only got to data structures in 2 semesters you had a truly terrible teacher.
Image
rawkies [r/trees]: nigga gimme 10 bitches and ill fuck all 10 --Snoop Doggy Dogg
セクシーのきたないお: words to live by
User avatar
rawkies
 
Posts: 256
Joined: Mon Oct 18, 2010 2:03 am
Location: San Leandro, CA

Re: For all you C++ coders out thar

by Echoplex » Fri Nov 05, 2010 1:10 pm

The Java API does all that stuff for you now so it's not very critical
Image

All stock errythang
User avatar
Echoplex
 
Posts: 676
Joined: Mon Oct 11, 2010 2:14 am
Location: Madison, WI


Return to Spam Fest



Who is online

Users browsing this forum: No registered users and 1 guest