By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

add a new member to struct

P: n/a
given a struct

typedef struct {
int a;
int b;
} Logstruct;

How can I add a new member to the struct

typedef struct {
int a;
int b;
int c;
} Logstruct;

I want to achieve this without changing the original header file where
the struct is defined. Also I do not want to inherit from the struct.
Is it possible?

Dec 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Ninan" <ni****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
given a struct

typedef struct {
int a;
int b;
} Logstruct;

How can I add a new member to the struct

typedef struct {
int a;
int b;
int c;
} Logstruct;

I want to achieve this without changing the original header file where
the struct is defined. Also I do not want to inherit from the struct.
Is it possible?


Not directly, but perhaps something like this would suffice:

struct newLogStruct
{
Logstruct ls;
int c;
};

-Mike
Dec 15 '05 #2

P: n/a

"Ninan" <ni****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
given a struct

typedef struct {
int a;
int b;
} Logstruct;

How can I add a new member to the struct

typedef struct {
int a;
int b;
int c;
} Logstruct;

I want to achieve this without changing the original header file where
the struct is defined. Also I do not want to inherit from the struct.
Is it possible?


You could contain that struct inside another struct. Otherwise, no. You
can't modify a struct's definition without... well... modifying the struct's
definition (which means modifying the header file).

By the way, you're using the old C-style struct definition, using a typedef.
The C++ way to declare a struct is:

struct myStruct;
{
int a;
int b;
int c;
};
-Howard

Dec 15 '05 #3

P: n/a
My aim was to have minimum code change. I think inheritance seems
better in this case

Dec 15 '05 #4

P: n/a

|| modifying the struct's definition (which means modifying the header
file).
For a POD "struct' defintion.

enum RadarModulation
{
Continuous_Wave, // CW
FM, // noise frequency
AM, // AM modulation
};
struct myStruct
{
unsigned int Val1;
unsigned int Val2;
unsigned int Arr[0x1000];
RadarModulation RM;
// more stuff
};

memsetting myStruct will be a - fast / sure way to initaliaze things to
zero?

Dec 15 '05 #5

P: n/a
ma740988 wrote:
|| modifying the struct's definition (which means modifying the header
file).
For a POD "struct' defintion.

enum RadarModulation
{
Continuous_Wave, // CW
FM, // noise frequency
AM, // AM modulation
};
struct myStruct
{
unsigned int Val1;
unsigned int Val2;
unsigned int Arr[0x1000];
RadarModulation RM;
// more stuff
};

memsetting myStruct will be a - fast / sure way to initaliaze things to
zero?


I'm confused. Are you changing the topic of the thread with this
question or are you expanding on what Howard said? Also, please
indicate whom you are quoting since not all people use Google Groups
(which, BTW, will quote messages and identify the author for you if you
click "show options" and then "Reply" in the revealed header).

As for std::memset, while it will work on some POD structs, you cannot
safely use such a structs/classes with pointer member(s) to dynamically
allocated memory (which count as PODs; see
http://www.parashift.com/c++-faq-lit...html#faq-26.7).
Virtual tables will also cause a problem if they are introduced, and
even if you don't use such features, the next programmer to touch the
code might. Thus, private data and constructors are to be preferred
generally speaking, and of course, std::vector is to be preferred to
arrays (see
http://www.parashift.com/c++-faq-lit...html#faq-34.1).

Cheers! --M

Dec 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.