Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialization of members when inheriting from extern C struct

In Mixing C and C++ Code in the Same Program the following example (slightly abbreviated here to the relevant parts) is given. Assume buf.h contains the following:

struct buf {
    char* data;
    unsigned count;
};

// some declarations of existing C functions for handling buf...

It is then recommended to use

extern "C" {
  #include "buf.h"
}

class mybuf : public buf {
public:
    mybuf() : data(0), count(0) { }

    // add new methods here (e.g. wrappers for existing C functions)...
};

in order to use the struct within C++ with added features.

However, this clearly will produce the following error:

error: class `mybuf' does not have any field named `data'
error: class `mybuf' does not have any field named `count'

The reasons for this are explained in How can I initialize base class member variables in derived class constructor?, C++: Initialization of inherited field, and Initialize parent's protected members with initialization list (C++).

Thus, I have the following two questions:

  1. Is the code provided just plainly wrong or am I missing some relevant aspect? (After all, the article seems to stem from a reputable source)
  2. What is the correct way to achieve the desired effect (i.e., turning a C struct into a C++ class and adding some convenience methods like, e.g., a constructor, etc.)?

Update: Using aggregation initialization as suggested, i.e.,

mybuf() : buf{0, 0} {}

works, but requires C++11. I therefore add the following question:

  1. Using C++03, is there a better way to achieve the desired outcome than using the following constructor?

    mybuf() {
      data = 0;
      count = 0;
    }
    
like image 757
godfatherofpolka Avatar asked Apr 20 '15 10:04

godfatherofpolka


People also ask

How do you initialize a member variable?

To initialize a class member variable, put the initialization code in a static initialization block, as the following section shows. To initialize an instance member variable, put the initialization code in a constructor.

How do you initialize a class member in C++?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

What is an initializer in C++?

An initializer specifies the initial value of a variable. You can initialize variables in these contexts: In the definition of a variable: C++ Copy. int i = 3; Point p1{ 1, 2 };


2 Answers

If you can use a c++11 compatible compiler then this would be a perfect use case for an initializer list using aggregate initialization.

mybuf() : buf{0, 0}
{}
like image 110
Simon Gibbons Avatar answered Sep 21 '22 12:09

Simon Gibbons


One "correct" way, if your compiler is C++11 capable, is to use e.g.

mybuf() : buf{0, 0} {}
like image 45
Some programmer dude Avatar answered Sep 19 '22 12:09

Some programmer dude