473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

add a new member to struct

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
5 10269

"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

"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
My aim was to have minimum code change. I think inheritance seems
better in this case

Dec 15 '05 #4

|| 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up...
3
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is...
1
by: Dmitri Vorobiev | last post by:
Hello group, Is it possible to check the presence of a structure member at compile time using only the C preprocessor? What I have in mind could look like the following. Suppose the...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
15
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member...
40
by: Steve Rencontre | last post by:
I can't for the life of me see how to do pointer-to-member when the member is actually part of an embedded structure. That is, if I have: struct S1 { int a; }; struct S2 { S1 s; int b; }; ...
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
14
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.