const struct initialization

G

Geiger Ho

Hi all,

Consider:

a.hh
====

struct A {
mytype_t attr1,
mytype_t attr2,
int attr3,
int attr4,

// some methods below
}


a.cc
====
extern const A DEFAULT_A = {attr1, attr2, attr3, attr4};

G++ compile with the following error:
'DEFAULT_A' must be initialized by constructor, not by `{...}'


How can I correct this?
Is it:
extern const A DEFAULT_A = *(new A(attr1, attr2, attr3, attr4));

And why it can't be that like C? I am just switched from C to C++, not very
familiar with C++.

Thanks in advance.

Regards,
 
A

Alan Johnson

Geiger said:
Hi all,

Consider:

a.hh
====

struct A {
mytype_t attr1,
mytype_t attr2,
int attr3,
int attr4,

// some methods below
}


a.cc
====
extern const A DEFAULT_A = {attr1, attr2, attr3, attr4};

G++ compile with the following error:
'DEFAULT_A' must be initialized by constructor, not by `{...}'

What you are trying to use is called an _aggregate_ in C++. An
aggregate is basically a struct (or class) as you know them from C. That
is, no constructors, no private/protected non-static data members, no
base class, and no virtual functions. It can however (if I read the
standard correctly) have public member functions that aren't
constructors (unlike C). For aggregates you can use the brace-enclosed
list to initialize it.

The C++ way to do it, however, is to provide a constructor for each way
you might initialize an object of the class. For instance, you might
provide the constructor:

A(const mytype_t &a1, const mytype_t &a2, int a3, int a4)
: attr1(a1), attr2(a2), attr3(a3), attr4(a4)
{}


You could then declare a constant of type A as:

const A DEFAULT_A(attr1, attr2, attr3, attr4) ;
How can I correct this?
Is it:
extern const A DEFAULT_A = *(new A(attr1, attr2, attr3, attr4));

This is definately not what you want to do. 'new' dynamically allocates
memory, analogous to malloc.
And why it can't be that like C? I am just switched from C to C++, not very
familiar with C++.

Thanks in advance.

Regards,

-Alan
 
M

Me

struct A {
mytype_t attr1,
mytype_t attr2,
int attr3,
int attr4,
semi-colons?

// some methods below
}
semi-colon?

extern const A DEFAULT_A = {attr1, attr2, attr3, attr4};

G++ compile with the following error:
'DEFAULT_A' must be initialized by constructor, not by `{...}'

the "some methods below" that you didn't paste probably contained a
constructor didn't it? In which case you cannot use aggregate
initialization on it.
How can I correct this?
Is it:
extern const A DEFAULT_A = *(new A(attr1, attr2, attr3, attr4));

Oh no, not this again. Every time you see a pointer, don't
automatically assume you need to allocate an object on the heap (not
related here, but I'm just throwing it out there) and don't use new
just because you're used to it from some other programming languages.
The above code would leak memory because you allocate a temporary
object, copied its value over, but you didn't store the pointer to the
temporary to delete later on. The correct way is:

extern const A DEFAULT_A(attr1, attr2, attr3, attr4);

assuming you have a constructor that can take those 4 arguments.
 
P

Prawit Chaivong

Geiger said:
Hi all,

Consider:

a.hh
====

struct A {
mytype_t attr1,
mytype_t attr2,
int attr3,
int attr4,

// some methods below
}


a.cc
====
extern const A DEFAULT_A = {attr1, attr2, attr3, attr4};

G++ compile with the following error:
'DEFAULT_A' must be initialized by constructor, not by `{...}'


How can I correct this?
Is it:
extern const A DEFAULT_A = *(new A(attr1, attr2, attr3, attr4));

And why it can't be that like C? I am just switched from C to C++, not very
familiar with C++.

Thanks in advance.

Regards,

Question:
1. Why do we need to initialise it. We tell compiler that 'A' is
somewhere else by using keyword 'extern', don't we?
2. struct DEFAULT_A has any constructor? if so, you cannot initialise
by '{}'

Regards,
 
G

Geiger Ho

Me said:
the "some methods below" that you didn't paste probably contained a
constructor didn't it? In which case you cannot use aggregate
initialization on it.

Yes, exactly.
Oh no, not this again. Every time you see a pointer, don't
automatically assume you need to allocate an object on the heap (not
related here, but I'm just throwing it out there) and don't use new
just because you're used to it from some other programming languages.
The above code would leak memory because you allocate a temporary
object, copied its value over, but you didn't store the pointer to the
temporary to delete later on. The correct way is:

extern const A DEFAULT_A(attr1, attr2, attr3, attr4);

assuming you have a constructor that can take those 4 arguments.

Thanks. This is really what I need.
 
Z

zx.zhangxiong

I'm so confused after I've test the code.

The following code can accepted by g++ v3.3.3 completely
and accepted by gcc V3.3.3 with one warning: warning: `DEFAULT_A'
initialized and declared `extern'.
I think it's because 'extern' declaration is no necessary here.

a.h
==
struct A { int i; int j};

a.c
==
extern const struct A DEFAULT_A = {1, 2 };

-----------------------------------------------------------------------------------------------

and more:

g++ can accept
extern const A DEFAULT_A = {1, 2};
but gcc can not.


What's the problem?
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top