Initializing member variables that are struts

2

2b|!2b==?

I have a class that contains C structs as member variables. By C
structs, I mean they cannot have ctors/dtors because they have C linkage
(extern "C").

For eg:

MyClass
{
//Impl
private:
MyStruct1 m_struct ;
};


//In C code, I could declare/define/initialize the m_struct variable
using 1 line:

MyStruct1 m_struct = {0} ;

In C++, I would have liked to resort to something similar (well
excluding the declaration part), using an initialization list like so:

MyClass()
:m_struct({0})
{}

Of course, this dosent work. The only way I found to initilalize a C
struct was to do the ff:

MyClass()
{
MyStruct m_struct = {0} ;
}

which compiles ok - but I can't help wondering if this is correct. It
looks like a declaration - which if it is, means that my member variable
did not get initialized. What does the C++ spec say about this ?
 
I

Ian Collins

2b|!2b==? said:
I have a class that contains C structs as member variables. By C
structs, I mean they cannot have ctors/dtors because they have C linkage
(extern "C").
The linkage isn't the issue, use by C code is.
For eg:

MyClass
{
//Impl
private:
MyStruct1 m_struct ;
};


//In C code, I could declare/define/initialize the m_struct variable
using 1 line:

MyStruct1 m_struct = {0} ;

In C++, I would have liked to resort to something similar (well
excluding the declaration part), using an initialization list like so:

MyClass()
:m_struct({0})
{}

Of course, this dosent work. The only way I found to initilalize a C
struct was to do the ff:

MyClass()
{
MyStruct m_struct = {0} ;
}

which compiles ok - but I can't help wondering if this is correct. It
looks like a declaration - which if it is, means that my member variable
did not get initialized. What does the C++ spec say about this ?
Does it? I'd expect at least one warning that the function scope
variable shadows the class member.

One tidy solution is to derive form the C sourced struct and provide a
default constructor in the derived struct:

struct UsedByC { int n; };

struct UsedByCpp : UsedByC {
UsedByCpp() { n = 0; }
};

class MyClass {
UsedByCpp m_struct;
};
 
R

Rolf Magnus

2b|!2b==? said:
I have a class that contains C structs as member variables. By C
structs, I mean they cannot have ctors/dtors because they have C linkage
(extern "C").

For eg:

MyClass
{
//Impl
private:
MyStruct1 m_struct ;
};


//In C code, I could declare/define/initialize the m_struct variable
using 1 line:

MyStruct1 m_struct = {0} ;

In C++, I would have liked to resort to something similar (well
excluding the declaration part), using an initialization list like so:

MyClass()
:m_struct({0})
{}

Of course, this dosent work. The only way I found to initilalize a C
struct was to do the ff:

MyClass()
{
MyStruct m_struct = {0} ;
}

which compiles ok - but I can't help wondering if this is correct.

Well, it's correct, but it doesn't seem to be what you want. This creates a
new local struct within the constructor and has nothing to do with the
member variable.
It looks like a declaration - which if it is, means that my member
variable did not get initialized.

That's right.
What does the C++ spec say about this ?

MyClass()
:m_struct()
{}
 
A

ajk

In C++, I would have liked to resort to something similar (well
excluding the declaration part), using an initialization list like so:
MyClass()
:m_struct( MyStruct() )
{ }
 
F

Fei Liu

2b|!2b==? said:
I have a class that contains C structs as member variables. By C
structs, I mean they cannot have ctors/dtors because they have C linkage
(extern "C").

For eg:

MyClass
{
//Impl
private:
MyStruct1 m_struct ;
};


//In C code, I could declare/define/initialize the m_struct variable
using 1 line:

MyStruct1 m_struct = {0} ;

In C++, I would have liked to resort to something similar (well
excluding the declaration part), using an initialization list like so:

MyClass()
:m_struct({0})
{}

Of course, this dosent work. The only way I found to initilalize a C
struct was to do the ff:

MyClass()
{
MyStruct m_struct = {0} ;
}

which compiles ok - but I can't help wondering if this is correct. It
looks like a declaration - which if it is, means that my member variable
did not get initialized. What does the C++ spec say about this ?


As it has been explained, as long as you only use the struct in C++
code, there is no problem defining constructors etc in your struct. It's
no different from a class except the default access is public.
 
J

James Kanze

Well, it's correct, but it doesn't seem to be what you want. This creates a
new local struct within the constructor and has nothing to do with the
member variable.
That's right.
MyClass()
:m_struct()
{}

That only works if he wants zero initialization (which
corresponds to the example he actually gave, of course). A more
general solution would be:

namespace {
MyStruct1 myStructInit = { ... } ;
}

MyClass()
: m_struct( myStructInit )
{
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top