SoftOver
 
Recommended


User login


 

Puzzles

   

Is this code valid? Why? What will be printed?

#include <stdio.h>

class C
{
  int n_;
  const char * s_;
public:
  C (const char * s) : n_(0),s_(s) {}
  operator const char * () { return s_; }
  operator int() { ++n_; return n_; }
};

int main()
{
  C c("Unformat %s %d\n");
  printf(c,c,c);
}
   

What will be printed? Why?

#include <iostream>

struct E
{
  void Do() {std::cout << "E Done" << std::endl;}
};
struct D
{
  void Do() {std::cout << "D Done" << std::endl;}
  E* operator->() {return &e;}
  E e;
};
struct C
{
  void Do() {std::cout << "C Done" << std::endl;}
  D* operator->() {return &d;}
  D d;
};
struct B
{
  void Do() {std::cout << "B Done" << std::endl;}
  C& operator->() {return c;}
  C c;
};
struct A
{
  void Do() {std::cout << "A Done" << std::endl;}
  B& operator->() {return b;}
  B b;
};

int main()
{
  A a;
  a->Do();
}
   

The code below is a valid C++. Why?

#include <stdio.h>

struct A
{
  A(bool) {}
};

class B{};

void print(const A&)
{
  printf("Boolish Ball...\n");
}


int main()
{
  B * p;
  print(p);
}
   

Please look at the following code and try to find a tiny bug.

#include <iostream>

class SoftwareDeveloper
{
public:
  SoftwareDeveloper(unsigned bugPerThousandLines) 
  : bugsPerThousandLines(bugsPerThousandLines)
  { /*...*/ }
  unsigned GetBugs(unsigned linesOfCode)
  { 
    return linesOfCode*bugsPerThousandLines/1000; 
  }
private:
  unsigned bugsPerThousandLines;   
};

int main()
{
  SoftwareDeveloper JohnDoe(10);
  std::cout << JohnDoe.GetBugs(555) << std::endl;  
}
Syndicate content