Jump to content


Dare to test your C++ knowledge here?


31 replies to this topic

#21 Dia

    DevMaster Staff

  • Administrators
  • 1120 posts

Posted 28 October 2004 - 08:19 PM

I like the questions. Some of them make good interview questions :)

#22 Dia

    DevMaster Staff

  • Administrators
  • 1120 posts

Posted 28 October 2004 - 08:44 PM

Here's some other good questions you can add to the list:

Q1) What is the difference between the following declarations:

const char *a
char const *a
char* const a;


Q2) If you have a base class and another class derived from the base. When calling a
 virtual function from the contructor, why does base ctor get the base's virtual function
 instead of the derived version?

Q3) When and why would you want to declare a virtual destructor?


#23 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1227 posts
  • LocationOttawa, Ontario, Canada

Posted 28 October 2004 - 10:17 PM

bladder said:

Got something. Not very fun to execute though...
The answer is really simple. Almost trivial actually. My question was to get a pointer to a constructor. Taking the definition of 'constructor' liberally, I just want a function that creates an object. The simplest way to do this is to have a static function like this:
class Foo
{
  Foo()
  {
    // Hello world!
  }
};

static Foo *fooConstructor()
{
  return new Foo();
}
fooConstructor can be used as a pointer, passed around to create the object we want without keeping track of the actual type of object we need. I don't recommend this approach if you can avoid it though. I once had to use it to construct an object from assembly code. I searched for several days how to get the pointer of the constructor into the assembly code, until I realized how simple it was...

#24 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 29 October 2004 - 02:53 AM

Dia Kharrat said:

Here's some other good questions you can add to the list:

Q1) What is the difference between the following declarations:

const char *a
char const *a
char* const a;


Q2) If you have a base class and another class derived from the base. When calling a
 virtual function from the contructor, why does base ctor get the base's virtual function
 instead of the derived version?

Q3) When and why would you want to declare a virtual destructor?

View Post



Q1) const char* is a pointer to constant data; char const* is also a pointer to "char const" ie: to constant data. Thrid one is a constant pointer to data (data can be changed but not pointer).

Q2) Because the derived class hasnt been created yet?

Q3) If you have the following situation
Renderer* r = new D3DRenderer();
delete r;

If you want r~D3DRenderer to be called then ~Renderer better be virtual.

#25 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 29 October 2004 - 03:52 AM

Interesting thread. Some of these questions are obvious, some abtruse...

Q 000) What's the difference between 'char* = "bah"' and 'char[] = "blah"'?

Besides the difference between "blah" and "bah"? I'm guessing that the second one is not null-terminated, am I right?

Q 005) What's the maximum value x can contain?

unsigned int x:5;

My guess is 31. The ':5' limits it to using 5 bits, correct?

Q 018) What does the following macro do?

#define macro(x, y) (int)(&((x*)0)->y)

That's kinda cool, actually..
reedbeta.com - developer blog, OpenGL demos, and other projects

#26 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 29 October 2004 - 05:51 AM

sorry ReedBeta, the blah thing was a typo. It's fixed now.

But as for your answer, no, they are both null terminated. The difference is that char[] allocated memory for 'x' number of characters (5 in this case) whereas char* just allocated memory to store a pointer, and *assumes* that it points to a valid string of characters. Furthermore, char[] does not point to const data and is more solid then char*. char* may point to const data, or it may not.

for example try doing this:

char a[] = "blah";
char* b = "blah";

strcpy( a, "halb" ); // will not crash
strcpy( b, "halb" ); // most probably will crash.

Quote

My guess is 31. The ':5' limits it to using 5 bits, correct?
correct.

#27 cdgray

    New Member

  • Members
  • PipPip
  • 21 posts

Posted 29 August 2005 - 04:43 AM

This is an excellent thread. Learned a lot by reading it :)

#28 baldurk

    Senior Member

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 29 August 2005 - 09:13 AM

cdgray said:

This is an excellent thread. Learned a lot by reading it :)

View Post


Don't post to topics that are almost a year old unless you have a really good reason. Saying you enjoyed the thread isn't.
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.

#29 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 02 September 2005 - 10:33 AM

Don't want to kick this thread, but since it's done already and not all the answers are posted yet, here's my list:

Q 000) The first defines a pointer to some statically allocated buffer, the second defines an array of 5 chars in the current scope.

Q 001) Use functions (primarily used for operators) defined in the same namespace as the type you use the operators on. Without koenig look-up, this wouldn't work:
#include <iostream>
#include <string>

int main ()
{
  std::string blah ("blah");
  std::cout << blah;
}
because the operator << (ostream &, const string &) is defined in the std namespace, and you're not explicitely importing everything from the std namespace into the global namespace using a using directive (using namespace std;)

Q 002)
template<class T> void foo(T * t);   // #1
template<class T> void foo(const T * t); // #2

int main()
{
  const int i = 3;
  foo(i); // calls #2 with T=int -not #1 with T=const int- because it is more specialized
}

Q 003) While primarily used to construct an object in a given piece of memory using a void * as second argument to new, it is practically any defined new operator with more than 1 parameter
char buf[sizeof(MyClass)];
new (buf) MyClass (123); // constructs a MyClass inside buf.

Q 004) It is called when the constructor throws an exception when constructed using a placement new operator

Q 005) 31

Q 006) Depends on the actual bitsize of short so this question is unanswerable, but assuming it's 16 it will output "==" and a newline.

Q 007) Nittpick answer: an incomplete if statement evaluates to nothing but a compile error :D

Serious answer: When x is some value type that evaluates to 1 or true (or when x is of some type T for which operator==(T, T) and operator==(bool, T) are defined and the latter returns true for some value of x ;))

Q 008) I'll ignore the custom types for x for now, x can be anything but a value that evaluates to 0 or false.

Q 009) Undefined.

Q 010) "0", followed by a newline, followed by "1", followed by a newline.

Q 011) By using a placement new and an explicit destructor call:
MyClass * c = new (buffer) MyClass;
c->~MyClass();

Q 012) Yes.

Q 013) A template specialization is a specialization of a template for a specific set of template parameter values. Partial specialization is a form of template specialization where a class (or struct) is specialized but not for a complete set of explicit template parameters.
template<class T> struct MyType { };
template<class T> struct MyType<T *> { }; // partial specialization for all pointers
template<> struct MyType<bool *> { }; // explicit specialization for bool*

Q 014)
int MultiplyBy321(int val)
{
  return (val << 8) + (val << 6) + val;
}

Q 015)
template <class T> struct TypeTraits
{
  static const bool isConst = false;
  // you probably want more definitions here
};

template<class T> struct TypeTraits<const T>
{
  static const bool isConst = true;
};

template<class T> void foo()
{
  if (TypeTraits<T>::isConst)
    std::cout << "T is const" << std::endl;
  else
    std::cout << "T is not const" << std::endl;
}
Or you could use the TypeTraits<T>::isConst as a template parameter to do more compile-time logic.

Q 016) Polymorphism, defining a derived type with more specialized functionality. Because a derived class inherits everything from it's base, it can be seen as a base.

Q 017)
std::ostringstream output;
std::ifstream input ("file.txt");
output << input.rdbuf();
output.str() now returns a std::string with the contents of the file, assuming everything worked out fine (you should check for errors on input)

Q 018) It returns the offset of a member in a struct/class. Note that this doesn't work if y is a bitfield or a type with an operator & that doesn't what you expect.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#30 edam

    New Member

  • Members
  • Pip
  • 7 posts

Posted 30 May 2012 - 02:21 PM

View Post.oisyn, on 02 September 2005 - 10:33 AM, said:

Q 009) Undefined.

Why is
int x = 2;
x += x++;
Why is the value of x undefined, exactly? Isn't this defined by operator precendence?

#31 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 30 May 2012 - 06:50 PM

Operator precedence has nothing to do with evaluation order, and evaluation order is undefined inbetween sequence points. The problem with that particular expression is whether the new value of x after the increment is written before or after the assignment. Both are perfectly acceptable according to the C++ standard. The resulting value of x can be 3 or 5, depending on compiler implementation:

int x = 2;
int tmp = x;
x = tmp + 1;
x += tmp;   // x == 5

// or

int x = 2;
int tmp = x;
x += tmp;
x = tmp + 1;  // x == 3

C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#32 alphadog

    DevMaster Staff

  • Moderators
  • 1716 posts

Posted 01 June 2012 - 01:43 PM

View Postbladder, on 28 October 2004 - 12:09 PM, said:

Try and do this without referencing anything to see how much you really know about c++. 18 questions all together. none of them have answers more the a line or two.

The funny (sad?) thing about a quiz like that is that you can find people who can answer all these didactic questions, yet still would not be able to code their way out of a box.
Hyperbole is, like, the absolute best, most wonderful thing ever! However, you'd be an idiot to not think dogmatism is always bad.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users