Four things you probably didn’t know about C++

Today I want to share a few “features” of C++ that you probably weren’t aware of. I placed the word features in quotes because most of them are confusing and there are only a few cases where they miht be useful. In many cases thrying to use these oddities will result in bad or at least unreadable code.

Case label placement

Did you know that case labels in C++ resemble the labels used by our old foe, the goto statement? We all know that goto is evil but it is still part of C++ language. Even modern compilers will compile the following code without any problems.

void badFunc() {
  for (int i=0; i<10; i++) {
    if (i>5) goto aargh;
    std::cout << i << std::endl;
  }
  return;
    
aargh:
  std::cout << "Aargh!" << std::endl;
  return;
}

See how we have defined a label called aargh. Inside the loop we transfer program execution to the code following that label using the goto aargh statement. Also notice how the colon : resembles the colon used in the case labels of a switch statement. The reason for this is that internally the compiler produces very similar code.

switch (number) {
  case 0: 
    doSomething();
  case 1:
  case 2:
    doSomethingElse();
}

We usually indent the case labels to make them look less like the old goto labels. In many situations the compiler treats them more or less the same. A standard way for the compiler to implement the switch statement is to use a jump table. The argument of the switch statement is used to look up the position of the corresponting case label. Much like goto, code execution is simply transferred to that point. This is also one reason why you need break statement if you only want part of the code after the label to execute.

One consequence of the way C++ understands case labels is that they can appear at any level within the switch statement. I have contrived this horrendous example which compiles and runs perfectly well in any modern C++ compiler.

#include <iostream>

void reallyBadCode() {
  int option;
  std::cin >> option;
  
  int N = 10;
  int i = 3;
  switch (option) {
    case 0:
      N = 5;
    case 1:
      for (i=0; i<N; ++i) {
    case 2:
        std::cout << i << std::endl;
      }
      break;
    default:
      std::cout << "Loop not executed" << std::endl;
  }
}

See if you can figure out what the code does before trying it out.

One real life example of the use of this language feature is Duff’s Device.

And one more little fact about labels. Did you know that you can place an arbitrary URL into your source code.

http://www.cogiolearning.co.uk

The protocol name including the colon define a label called http. What follows is a double slash so the remainder of the URL is treated as a comment.

Assigning to ternary statement

Now we probably all know the ternary statement condition?ifTrue:ifFalse and we regularly use it to assign values based on some condition.

a = (c=='y'?-1:1);

But did you know that ternary statements can evaluate to an lvalue. If you don’t remember what an lvalue is, it is a value that you can assign a value to. Or in other words a value that can be placed left of the assignment operator. Consider the following example.

void ternary_assign() {
  int option;
  std::cin >> option;
  
  int a = 0;
  int b = 0;
  
  (option>1?a:b) = 1;
  
  std::cout << "Values of a and b: " << a << " " << b << std::endl;
}

Here the value of the option variable determines which variable we assign to. If option is greater than 1, we assign a value to a and otherwise to b. But this is not the end of it. You can also use it select an object and call a method on that object.

class Person {
  private:
    std::string name_;
  public:
    Person(std::string name) : name_(name) {}
    void printName() {
      std::cout << "My name is " << name_ << std::endl;
    };
};

void ternary_object_selection() {
  int option;
  std::cin >> option;
  
  Person a("Alice");
  Person b("Bob");
  
  (option>1?a:b).printName();
  
}

And finally remember that function names in C++ can be used as function pointers. These are pointers to the memory address where the compiled code of the function body is stored. Useing the ternary statement we can select a function pointer and call the function associated with it. Here is an example.

void ternary_function() {
  double x;
  std::cin >> x;
  
  double result = (x>0.0?sin:cos)(x);
  
  std::cout << "Result is " << result << std::endl;
}

Note that we are not calling the sin or cos function inside the ternary statement but the ternary statement evaluates to a function pointer. Only after that has been evaluated the function is actually called.

The array operator is associative.

You know that one of the features of C and C++ is pointer arithmetic. Some love it and some hate it. But did you know that behind the scenes both languages use pointer arithmetic all the time. Remember that C style arrays can used like pointers, and also the other way around. In fact C++ makes almost no distinction between the two. You might have already geussed it, when you access an element from a plain array C++ uses pointer arithmetic to retrieve the element. The expression arr[3] is synonymous to *(arr+3). Here arr is treated as a pointer. We add three to the pointer and dereference to get the third element. But instead of *(arr+3) we can also write *(3+arr), right? This leads us to suspect that you might be able to write 3[arr] and, indeed, you can.

Here is a piece of code for you to try out.

void array_assoc()
{
  double values[5] = {0.0, 1.0, 3.14159, 1.41421, 1.61803};
  
  double x = 2[values];
  
  std::cout << "The value is " << x << std::endl;
}

Of course the array index does not have to be a literal but can be any integer expression.

void array_assoc()
{
  double values[5] = {0.0, 1.0, 3.14159, 1.41421, 1.61803};
  
  int n;
  std::cin >> n;
  
  double x = ((n>0?n:-n) % 4)[values + 1];
  
  std::cout << "The value is " << x << std::endl;
}

For good measure I have added used some obvious pointer arithmetic inside the square brackets. This is of course allowed.

Pure virtual functions with function body

Now we are coming to a feature that has a more C++ feel to it, pure virtual functions. You learn about pure virtuals in the context of defining interfaces and many textbooks claim that you can’t instantiate an object containing a pure virtual because the function doesn’t have a function body and the compiler wouldn’t know what to call. That is not completely correct. True, you can’t instantiate an object with a pure virtual. Nonetheless, the pure virtual can be implemented and also called from derived classes.

Here is a full working example.

#include <iostream>

class Base {
  public:
    virtual void doSomething() = 0;
    virtual ~Base() {}
};

class Derived : public Base {
  public:
    void doSomething();
};

void Base::doSomething() {
  std::cout << "Hello World!" << std::endl;
}

void Derived::doSomething() {
  Base::doSomething();
}

int main() {
  Derived d;
  d.doSomething();
  return 0;
}

In contrast to the previous examples, this feature can actually be useful in some contexts. From Scott Meyers book “Effective C++”:
Derived classes that implement this pure virtual function may call this implementation somewhere in their code. If part of the code of two different derived classes is similar then it makes sense to move it up in the hierarchy, even if the function should be pure virtual.

10 Comments

  1. Keith A. Lewis

    Mathematicians use the word ‘associative’ to mean (ab)c = a(bc), but I can’t think of a word to express the fact a[j] is the same as *(a+j).

    Your last example missed the opportunity to point out the useful https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface.

    Reply
    1. Rob G

      @Keith,

      The point is that a[j] is the same as j[a], so it is associative.

      Reply
      1. Micha

        I believe [] is commutative ( like a operator+: a+b=b+a)

        Reply
        1. Ivan

          Agreed. This is not associativity, but is based on commutativity of the + operator.

          It goes something like this a[i] *(a+i) *(i+a) i[a]

          Reply
  2. dissertation editing service

    That is not completely correct. True, you can’t instantiate an object with a pure virtual

    Reply
  3. super mario world

    Great information. Thank you.

    Reply
  4. paper writing service

    You are completely right in saying that these things were completely unfamiliar to me before reading this article.

    Reply
  5. Susan James

    C is a standout among st the most prominent programming dialects. It is broadly utilized on a wide range of programming stages, and there are few PC models for which a C compiler does not exist. C has significantly impacted numerous other prevalent programming dialects, most quite C++, which initially started as an expansion to C. whiteboard animation video services

    Reply
  6. John Martin

    Solitary outcome of the system C++ comprehends container marks is to facilitate they preserve show up at Assignment Writing Help several intensity inside the toggle articulation. Simply behind that comprise be assessed the capacity is really describe.

    Reply
  7. five nights at freddy’s

    Your article helped me understand a lot of information that was previously not for me understandable and difficult to perceive.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>