Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C struct member default values in struct

330 views
Skip to first unread message

Rick C. Hodgin

unread,
Oct 21, 2015, 11:31:35 PM10/21/15
to
Is there a version of C or C++ extension which takes this:

struct SAbc
{
int a;
int b;
int c;
};

struct SAbc myAbcVar[] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

And lets me change my structure to this:

struct SAbc
{
int a;
int b;
int c;
int d;
};

Without having to do this manually:

struct SAbc myAbcVar[] =
{
{ 1, 2, 3, -1 },
{ 4, 5, 6, -1 },
{ 7, 8, 9, -1 }
};

By allowing me instead to do something like this:

struct SAbc
{
int a;
int b;
int c;
int d = -1; // Default all unspecified values to -1
};

?

Does C++ or another C-like language have the ability to either
initialize all members in the struct definition, or to initialize
those members which are added on to the end later, as with C++'s
ability to initialize unspecified parameter values?

Best regards,
Rick C. Hodgin

supe...@casperkitty.com

unread,
Oct 22, 2015, 12:21:45 AM10/22/15
to
On Wednesday, October 21, 2015 at 10:31:35 PM UTC-5, Rick C. Hodgin wrote:
> Is there a version of C or C++ extension which takes this...

It's possible to do a lot with the preprocessor, though in some cases you
may have to define a macro called something like "COMMA" to ensure that you
can embed commas in within a macro argument. I've done used macros to let
met me define default values for a structure at the same time as I defined
the structure itself (that was especially useful before named-field initial-
izers were added to the syntax). I'm not sure how best to set up the macros
for what you have in mind, but it would seem fairly straightforward.

Frank Tetzel

unread,
Oct 22, 2015, 7:38:24 AM10/22/15
to
C++11 with initializer lists:

struct SAbc
{
int a;
int b;
int c;
int d = -1;

SAbc(int a, int b, int c) : a(a), b(b), c(c) {}
SAbc(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {}
};

SAbc myAbcVar[] { // = is optional
{0, 1, 2},
{0, 1, 2, 3}
};

Regards,
Frank

Rick C. Hodgin

unread,
Oct 22, 2015, 9:58:53 AM10/22/15
to
Is the "=" optional in C? Or just C++11?

-----
The rest of my response is off-topic for the C group, but if you're
interested in seeing my CAlive implementation, go here:

https://groups.google.com/d/msg/rapid_development_compiler/FjprgbxfAiU/r_-lVoi0CgAJ

Frank Tetzel

unread,
Oct 22, 2015, 11:27:59 AM10/22/15
to
> > C++11 with initializer lists:
> >
> > struct SAbc
> > {
> > int a;
> > int b;
> > int c;
> > int d = -1;
> >
> > SAbc(int a, int b, int c) : a(a), b(b), c(c) {}
> > SAbc(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {}
> > };
> >
> > SAbc myAbcVar[] { // = is optional
> > {0, 1, 2},
> > {0, 1, 2, 3}
> > };
>
> Is the "=" optional in C? Or just C++11?

Not in C as far as i know. It's new in C++11, see:

http://en.cppreference.com/w/cpp/language/list_initialization
http://en.cppreference.com/w/cpp/language/aggregate_initialization

Rick C. Hodgin

unread,
Nov 4, 2015, 2:06:16 PM11/4/15
to
Because member default values are introduced, CAlive will also introduce
non-constructor instantiation of class objects when used as follows:

char invalid_data[] = "Invalid";

class CAbc
{
public:
CAbc(char* p) : m_p(p) { }
~CAbc() { }

private:
char* p = invalid_data;
}


void function(void)
{
// No constructor will be called, m_p initialized to "Invalid"
CAbc* abc = lnew CAbc;

// Constructor will be called, m_p initialized to "Rick"
CAbc* abc = lnew CAbc("Rick");

Rick C. Hodgin

unread,
Nov 4, 2015, 2:07:29 PM11/4/15
to
On Wednesday, November 4, 2015 at 2:06:16 PM UTC-5, Rick C. Hodgin wrote:
> private:
> char* p = invalid_data;

Should be m_p, not p.
0 new messages