0

// I have a structure

 typedef struct
 {
    uint8_t* data;
    uint16_t data_ln;
 } Struc_Data;

//a derived type from this struct

 Struc_Data    tx_struct;

// i have to initialize this 'tx_struct' in constructor initializer list, i am not getting how

 Constr::Constr( ):
    tx_struct  (reinterpret_cast<uint8_t*>(nullptr), 0U)  //this does not work
 {

 }
  • What you've failed to do is show us Constr, and how you've left a declaration for Struct_Data out of its definition. Could you post the whole code? Its not that large. – Dan Apr 09 '14 at 06:44

2 Answers2

2

Struc_Data is an aggregate, so value-initialization will do if you want to zero-initialize the members:

Constr::Constr( ): tx_struct() {} // or tx_struct{}

Otherwise, use curly-brace initialization:

Constr::Constr( ): tx_struct{nullptr, 42U} {}

Here's a simplified, compiling example:

#include <stdint.h>

typedef struct
{
  uint8_t* data;
  uint16_t data_ln;
} Struc_Data;

struct Foo
{
  Foo() : tx_struct{nullptr, 0U} {}
  Struc_Data tx_struct;
};

Note that in C++ it is unusual to use the typedef syntax for class definitions. The preferred form is

struct Struc_Data { .... };
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks jaunchopanza but its Not working with m_tx_struct{}, tx_struct() {} OR m_tx_struct {reinterpret_cast(nullptr), 0U} {}. What i tried earlier 'tx_struct (reinterpret_cast(nullptr), 0U)', something is missing here itself :( – learning fellow Apr 09 '14 at 05:39
  • @learningfellow What is not working and who told you to use `reinterpret_cast`? – juanchopanza Apr 09 '14 at 05:40
  • @ juanchopanza: reinterpret_cast is used here to initialize the uint8_t* pointer to nullptr. We may not use it. Simply keeping 'tx_struct (nullptr, 0U) {}', code does not compile. – learning fellow Apr 09 '14 at 05:48
  • @learningfellow Then maybe your compiler does not support these C++11 features. The first variant should work pre-C++11. And you don't need `reinterpret_cast`. Note that the example I posted compiles with gcc 4.8.1. – juanchopanza Apr 09 '14 at 05:48
  • May be. I am using IAR compiler. with 'm_tx_struct ( nullptr, 0U )' i get two errors " Error[Pe415]: no suitable constructor exists to convert from int to "Struc_Data" Error[Pe018]: expected a ") " – learning fellow Apr 09 '14 at 06:09
  • @learningfellow OK, well, `nullptr` is C++11, and so is the curly brace initialization in the constructor initialization list. But if you just need to zero-initialize the struct's members, then use the first version I posted. – juanchopanza Apr 09 '14 at 06:10
  • Thanks juanchopanza but No, i have tried that too. I am very close with "tx_struct() ( nullptr, 0U )". one error remains Error[Pe130]: expected a "{". Tried {} with many combinations :P Wondering where it is required :( – learning fellow Apr 09 '14 at 06:53
  • @learningfellow If the first version doesn't work, then your compiler isn't even C++03 compliant WRT this very basic feature. It will be very hard to *guess* what will work for you without good knowledge of that particular compiler. Both code samples I provided are valid C++, but the second one requires C++11. – juanchopanza Apr 09 '14 at 06:56
0

Another solution:

  1. Change the C style struct to a C++ style struct.
  2. Define a constructor with the required arguments.

    struct Struc_Data
    {
      Struc_Data(uint8_t* data1, uint16_t data2) : data(data1), data_In(data2) {}
      uint8_t* data;
      uint16_t data_ln;
    };
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • It's not an anonymous `struct`. It just uses C-style `typedef` syntax. – juanchopanza Apr 09 '14 at 05:39
  • @juanchopanza Per my understanding, it is anonymous struct since there is no name before the '{`. Perhaps I am mistaken. – R Sahu Apr 09 '14 at 05:43
  • You could be right, but to me an anonymous struct really has no name. For example `struct {int i, double x} data;`. Here `data` is an instance of an anonymous struct. – juanchopanza Apr 09 '14 at 05:45
  • I am not allowed to change the struct in this case, it belongs to the stack bought. I am initializing in the Constructor Initializer list because PC-lint want it :( – learning fellow Apr 09 '14 at 05:53