|
C++ interview preparation: |
nonnus29
Member #2,606
August 2002
|
I wrote the following class to test when constructors are called for classes on the stack and got an error: class Test { private: static int count; public: Test() { cout << "created Test: " << Test::count << endl; Test::count++; } }; int main() { Test t1; Test* t2 = new Test(); } The error was: undefined reference to Test::count. Anyone know what the issue is? |
Jakub Wasilewski
Member #3,653
June 2003
|
You have to actually define the static outside the class in some .cpp file. The static is created only once (as opposed to other members) so it has to have a linker object to live in. int Test::count = 0; Still, it is fiendishly annoying and I always get that error and fix it after the compiler barfs . --------------------------- |
kazzmir
Member #1,786
December 2001
|
You have to put 'int count' somewhere outside the class. class Test{ ... } int Test::count; [edit] slow.. But anyway that code wouldn't count constructors on the stack, you would probably have to redefine the new operator to count the number of dynamically created objects and then do count - dynamic_count. |
Thomas Fjellstrom
Member #476
June 2000
|
Quote: But anyway that code wouldn't count constructors on the stack By virtue of it counting all constructions, it'll count objects on the stack. -- |
kazzmir
Member #1,786
December 2001
|
Ok it will count the constructors on the stack but it won't give you a right answer. The number of objects on the stack is probably also contained in the following: unsigned int stack_objects = MAXINT; Anyway, this code seems to work
Then purely stack objects are count - dynamic. |
ImLeftFooted
Member #3,935
October 2003
|
Wouldn't it be a hell of a lot simpler and cleaner to just use the ctors?
edit: Wait I see now, we're counting just stack instances... interesting |
nonnus29
Member #2,606
August 2002
|
Thanks Jakub and Kazz. Yes, I was just curious in what order constructors where called for static objects, I had the heap allocation in there just because. Turns out they're called in order and in between intevening statements which was a surprise. I assumed all stack objects would be instantiated when the stack frame was allocated, but apparently not so...... cout<< "something" << endl; Test t1; cout<< "something 2" << endl; test t2; Output: somthing |
Andrei Ellman
Member #3,434
April 2003
|
Kazzmir said:
Quote:
And finally, what's wrong with this code Nothing obvious that I can see. Is it something ridiculous due to the extern? Nothing due to extern. I only used extern because I wanted to keep the functions as black boxes. AE. -- |
kazzmir
Member #1,786
December 2001
|
Oh, I guess its the fact that bool does not force a value of 0 or 1 so if one function returned true as 2 and another returned true as 3 then (b1 == b2 ) would be false when the proper test is if ( b1 != FALSE && b2 != FALSE ){ } But given the code, I would think that foo and bar would return either TRUE or FALSE anyway.. |
Jakub Wasilewski
Member #3,653
June 2003
|
Well, if it is a C++ bool then the only actual values they take are true and false, so the whole point is moot: void foo() { bool a = 12; bool b = 14; if (a == b) printf("Yo!"); } The 12 and the 14 are automatically casted to bool, and the text appears on screen. That's how it works in C++, and I assume that's also the way it is in C99 (and there are no bools before then, IIRC). --------------------------- |
Kitty Cat
Member #2,815
October 2002
|
Quote: Well, if it is a C++ bool then the only actual values they take are true and false, so the whole point is moot:
if(!a == !b) -- |
Andrei Ellman
Member #3,434
April 2003
|
kazzmir said: Oh, I guess its the fact that bool does not force a value of 0 or 1 so if one function returned true as 2 and another returned true as 3 then (b1 == b2 ) would be false when the proper test is Is the right answer. The two functions are black boxes and you have no idea exactly how the bools are composed. They could be the result of applying a bitmask to some set of flags. One optimization you could do if you're 100% sure that the bools in question are either 0 or -1: if(!(b1 ^ b2)) { }
kazzmir said:
if ( b1 != FALSE && b2 != FALSE ){ }
This does not take into account when both of them are false. I would do what Kitty Cat did in this case. Jakub Wasilewski said: The 12 and the 14 are automatically casted to bool, and the text appears on screen. That's how it works in C++, and I assume that's also the way it is in C99 (and there are no bools before then, IIRC). Oops. Didn't take into account C++'s bool. Does C++ overload the assignment operator to convert all non-zero values to true, or does it overload the == operator for bool to use something similar to what Kitty Cat posted? AE. -- |
Jakub Wasilewski
Member #3,653
June 2003
|
Quote: Didn't take into account C++'s bool. Does C++ overload the assignment operator to convert all non-zero values to true, or does it overload the == operator for bool to use something similar to what Kitty Cat posted? As far as I know, neither. The bool type can only hold two (abstract, non-numeric) values. What C++ does is define an implicit cast from int to bool, which renders any non-zero int as true, and 0 as false. That implicit cast also happens when you use an int in an if or a while. You can also convert true and false back to int if that is needed, but that will always yield the same value for true (I'm not sure whether it's defined in the standard or left for the implementors to choose). I just assumed you meant one of the built-in bools because there were no typedefs anywhere. Both C++ bool and the C99 bool are guaranteed to work with your code. --------------------------- |
piccolo
Member #3,163
January 2003
|
wow |
ImLeftFooted
Member #3,935
October 2003
|
Richard Phipps
Member #1,632
November 2001
|
Stop thinking too much. Just do the interview. |
nonnus29
Member #2,606
August 2002
|
Thanks DD! Tomorrows the day, I'll let you guys know what the questions were.... for the sake of completeness. |
kazzmir
Member #1,786
December 2001
|
Some of the questions on that interview site are really lame. Anyway I thought this was funny http://dev.fyicenter.com/Interview-Questions/CPP-1/Anything_wrong_with_this_code_T_p_0_delet.html said:
Anything wrong with this code? Yes, the program will crash in an attempt to delete a null pointer. Yea.. nice work. http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.8 |
nonnus29
Member #2,606
August 2002
|
Well I just came back from the "interview". It wasn't really an interview, they stuck me in a room with a specification and I wrote some code on paper. If they like it, I'll get called back for a interview with the team. They had me write a queue using a linked list that stored a Document type that was abstract and I had to extend to two concrete types. I wrote a doubly linked list. I just remembered, in my haste I added an instance of the abstract class to the queue. Hmmm, maybe I shouldn't plan on getting a call back....
|
Thomas Fjellstrom
Member #476
June 2000
|
Quote: I just remembered, in my haste I added an instance of the abstract class to the queue. Bugs like that are normal... IF thats the only thing you got wrong, you probably did much better than other candidates. -- |
Richard Phipps
Member #1,632
November 2001
|
I don't even know what a queue is (In C++ terms..) |
OICW
Member #4,069
November 2003
|
It's a FIFO data structure. Basically a linked list, with two specifics. You add new objects on one end, and remove them from the other. It is analogous to a queue that occurs when people are waiting outside the theathre or whatever. [My website][CppReference][Pixelate][Allegators worldwide][Who's online] |
bamccaig
Member #7,536
July 2006
|
OICW said: It is analogous to a queue that occurs when people are waiting outside the theathre or whatever. I don't think English-as-a-first-language speakers would ever refer to a line up as a queue. Queue is a term I didn't know of until I learned about it in programming terms, although you could use a line up as an example (first come, first served). -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Jonatan Hedborg
Member #4,886
July 2004
|
Quote: I don't think English-as-a-first-language speakers would ever refer to a line up as a queue. Queue is a term I didn't know of until I learned about it in programming terms Actually, it's because you are a dirty cheeseburger-inhaling yank (no offense )
|
Thomas Fjellstrom
Member #476
June 2000
|
Quote: Actually, it's because you are a dirty cheeseburger-inhaling yank (no offense ) Indeed. Queue is used to refer to line ups in the UK all the time. Just watch any BBC show. A Queue is essentially just a FIFO list. -- |
|
|