const struct initialization



 DEVELOP > c-Plus-Plus > const struct initialization

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Geiger Ho"
Date: 05 Jul 2005 12:02:05 AM
Object: const struct initialization
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,
.

User: "Me"

Title: Re: const struct initialization 05 Jul 2005 01:35:21 AM

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.
.
User: "Geiger Ho"

Title: Re: const struct initialization 05 Jul 2005 06:18:48 AM
Me wrote:

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.

Yes, exactly.

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.

Thanks. This is really what I need.
.
User: ""

Title: Re: const struct initialization 05 Jul 2005 08:54:19 AM
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?
.



User: "Prawit Chaivong"

Title: Re: const struct initialization 05 Jul 2005 03:01:08 AM
Geiger Ho wrote:

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,
.

User: "Alan Johnson"

Title: Re: const struct initialization 05 Jul 2005 12:14:08 AM
Geiger Ho wrote:

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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER