473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Initializing member variables that are struts

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 ?

May 1 '07 #1
5 7398
2b|!2b==? wrote:
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;
};

--
Ian Collins.
May 1 '07 #2
2b|!2b==? wrote:
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()
{}
May 1 '07 #3
ajk
On Tue, 01 May 2007 10:42:54 +0100, "2b|!2b==?" <ro**@your.box.com>
wrote:
>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() )
{ }

May 1 '07 #4
2b|!2b==? wrote:
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.
May 1 '07 #5
On May 1, 11:55 am, Rolf Magnus <ramag...@t-online.dewrote:
2b|!2b==? wrote:
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 createsa
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()
{}
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 )
{
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member...
0
by: Adam Smith | last post by:
Are there any drawbacks to initializing static member variables for classes used in an asp.net application within application_start? My classes have arraylist like tables, which cache information...
4
by: Jamie Hankins | last post by:
I'm probably being dense here. In the following situation: class Base { int x; int y; } class Decendant : Base { int z; }
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
1
by: nithiya sri | last post by:
i am new to struts.I try running struts application in Eclipse with the tomcat plug in .. it shows the following error:; please help me in this... SEVERE: Error loading WebappClassLoader ...
14
by: Sugandh Jain | last post by:
Hi, The warning from Microsoft.Performance Code Analysis check that, its not required to initialize numeric variables to zero, boolean to false and object to null is a good one because CLR does...
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.