Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » C++ interview preparation:

This thread is locked; no one can reply to it. rss feed Print
C++ interview preparation:
nonnus29
Member #2,606
August 2002
avatar

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
avatar

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 :).

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

kazzmir
Member #1,786
December 2001
avatar

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
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

kazzmir
Member #1,786
December 2001
avatar

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

1class Test {
2 public:
3 static int count;
4 static int dynamic;
5 public:
6 Test() {
7 cout << "created Test: " << Test::count << endl;
8 Test::count++;
9 }
10 
11 void * operator new( size_t n ){
12 dynamic += 1;
13 return malloc(n);
14 }
15};

Then purely stack objects are count - dynamic.

ImLeftFooted
Member #3,935
October 2003
avatar

Wouldn't it be a hell of a lot simpler and cleaner to just use the ctors?

1class Test {
2 public:
3 static int count;
4 public:
5 Test() {
6 cout << "created Test: " << Test::count << endl;
7 Test::count++;
8 }
9 Test(const Test &test) {
10 cout << "copied Test: " << Test::count << endl;
11 Test::count++;
12 }
13 ~Test() {
14 cout << "lost Test: " << Test::count << endl;
15 Test::count--;
16 }
17};
18 
19... in test.cpp ...
20 
21int Test::count = 0;

edit: Wait I see now, we're counting just stack instances... interesting

nonnus29
Member #2,606
August 2002
avatar

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
created Test 1
something 2
created Test 2

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.

--
Don't let the illegitimates turn you into carbon.

kazzmir
Member #1,786
December 2001
avatar

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
avatar

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).

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

Kitty Cat
Member #2,815
October 2002
avatar

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)
would work too, for bools and integers, and should be able to be properly optimized by a compiler.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

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.

--
Don't let the illegitimates turn you into carbon.

Jakub Wasilewski
Member #3,653
June 2003
avatar

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.

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

piccolo
Member #3,163
January 2003
avatar

wow
-------------------------------
i am who you are not am i

ImLeftFooted
Member #3,935
October 2003
avatar

Richard Phipps
Member #1,632
November 2001
avatar

Stop thinking too much. Just do the interview.

nonnus29
Member #2,606
August 2002
avatar

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
avatar

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?
T *p = 0;
delete p;

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
avatar

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
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Richard Phipps
Member #1,632
November 2001
avatar

I don't even know what a queue is (In C++ terms..) :)

OICW
Member #4,069
November 2003
avatar

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]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

bamccaig
Member #7,536
July 2006
avatar

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).

Jonatan Hedborg
Member #4,886
July 2004
avatar

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 :D)

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Actually, it's because you are a dirty cheeseburger-inhaling yank (no offense :D)

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730



Go to: