add a new member to struct

N

Ninan

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?
 
M

Mike Wahler

Ninan said:
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
 
H

Howard

Ninan said:
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
 
N

Ninan

My aim was to have minimum code change. I think inheritance seems
better in this case
 
M

ma740988

|| 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?
 
M

mlimber

ma740988 said:
|| 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-lite/intrinsic-types.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-lite/containers.html#faq-34.1).

Cheers! --M
 

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,142
Latest member
arinsharma
Top