Declaring a stack of a struct inside the struct definition [duplicate] - c++

This question already has answers here:
Can standard container templates be instantiated with incomplete types?
(3 answers)
Closed 8 years ago.
I need to declare two stacks of a struct inside of it's own struct declaration. I know I could do this operation with an array as long as I reference it with a pointer inside the struct (i.e. FA *goingTo[30]; would give me an array of 30 FAs). Is there a similar way to reference a stack?
typedef struct FA
{
std::stack<FA> goingTo;
std::stack<FA> comingFrom;
};

The stack objects that you are defining in the struct would themselves contain (possibly) multiple instances of the struct, each instance containing its own stacks which again contain more of the structs. So if you think about it, this is an infinite chain of containment. You can modify the definition (and the usage) to contain stacks of pointers to FA*. This would solve the problem.
typedef struct FA {
std::stack<FA*> goingTo;
std::stack<FA*> comingFrom;
};

Related

Get type of instantiated unnamed struct [duplicate]

This question already has answers here:
Can an unnamed struct be made static?
(2 answers)
Passing anonymous structures to functions as parameter using C++ [duplicate]
(3 answers)
Closed 9 months ago.
I'm writing an app in C++ that interfaces with some code written in C.
The following is a simplified version of a struct defined in C code that I can't modify.
struct event{
uint8_t type;
union {
struct /* WishThisHadAName */ {
// ...
} connect;
struct {
// ...
} disconnect;
};
};
I'm trying to be able to define functions that take a pointer to the different unnamed structs, connect, disconnect, and all the others not listed above. To look something like the following.
void onConnect( struct WishThisHadAName *evt );
Is it possible to create this prototype without modifying the original C struct? Is there an easy way to create a wrapper for the unnamed structs to give them a name? Am I forced to create named structs that mirror the unnamed ones I'm trying to use?
I know you can get the type of an instantiated variable with decltype but I can't figure out how I could use that or declval in order to create the function prototype I'm looking for.
Simple as using event_connect_t = decltype(event::connect);.
Then you can use it as void onConnect( event_connect_t *evt );.
You can't declare a compatible type, but you can just extract the existing type declaration from the definition. decltype can resolve static member references just fine.
If you're using a compiler such as gcc/g++, you can use typeof to create a typedef for the anonymous type:
typedef __typeof__(((struct event){0}).connect) conn_type;
typedef __typeof__(((struct event){0}).disconnect) disconn_type;
Or without compound literals:
struct event e;
typedef __typeof__(e.connect) t1;
typedef __typeof__(e.disconnect) t2;

C++ iterate through struct by increasin pointer bit by bit [duplicate]

This question already has answers here:
Iterate through Struct and Class Members [duplicate]
(6 answers)
Closed 5 years ago.
I am currently trying to iterate through a struct var in C++. I've got a struct var but I need to access specific elements of the struct.
someStruct {
int a;
int b;
bool c;
...
};
&someStructVar+1 would increase the memory by the size of the struct. But I need to increase the memory address by one bit after another. Is this possible? Is there any other approach?
What you're asking for is reflection, which C does not support. You can't get a list of fields of a struct and iterate through them.
You'll need to explicitly call out each field by name.
You can't "iterate" members of struct through pointer arithemetics, this is not allowed in C++.
The only thing you can iterate with pointer arithemtics are C-style arrays.

C++ Proper use of struct in function arguments [duplicate]

This question already has answers here:
using struct keyword in variable declaration in C++
(6 answers)
Closed 6 years ago.
Quick Question about passing a struct as a function arg. Maybe its a carry over from C that makes no difference in C++.
In many Linked List examples you see them re-declare the word struct in any function argument that takes a struct and I don't understand why. Or any allocation for that matter. A structure is its own object and declaring just the name of the struct is sufficient.
Example:
struct Node{
int data;
Node * next;
};
// Function to output data.
void printNode(struct Node* myNode){
// Print data of node
}
why is the word struct re-declared in the function arg. Declaring type Node* works just fine.
Thanks.
There is a difference between C and C++ use of declarations produced with struct keyword:
In C, struct defines a new struct tag, but not a new type
In C++, struct defines a new type.
The consequence is that C++ lets you use the name defined in struct, while C requires you to either prefix a struct to the tag, or use typedef to define a new type.
C requires it. C++ does not require it, but allows it for backward compatibility. Do not do this for new code.

Struct and bitfield member ordering [duplicate]

This question already has answers here:
Do class/struct members always get created in memory in the order they were declared?
(6 answers)
Closed 6 years ago.
Is the order of members in a) in a struct b) in a bitfield guaruanteed?
In other words, given a certain member of a struct or a bitfield, am I guaranteed that its offset from the beginning of the struct/bitfield will be no less than the sum of the sizes of the members that preceded it?
To give an example:
struct S{
char a[N];
unsigned b : M;
char c : O;
};
Will the offset of c be at least sizeof(a)+sizeof(b)?
Yes.
C++ standard:
Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object.

Forward declaring a typedef of an unnamed struct [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forward declarations of unnamed struct
If I have
typedef struct tagPAGERANGE
{
int iFirstPage;
int iLastPage;
} PAGERANGE;
I can forward declare it that way
struct tagPAGERANGE;
typedef struct tagPAGERANGE PAGERANGE;
But what I have is
typedef struct
{
int iFirstPage;
int iLastPage;
} PAGERANGE;
I'm not sure how I can do it. I only want to hold a pointer to this struct. Right now I'm stuck with either including a rather substantial header, or duplicating the definition of the struct.
It's impossible. You can only declare named structs.
Think about what identifies a struct that doesn't have a name, and how do you tell the compiler that it's that struct you want. If it doesn't have a name, it's identified by its members, so you need to provide members — i.e. define it. Therefore, you can't just declare it — you don't have a luxury of an identifier other than the definition itself.
Since this is used in a C++ code, just get rid of the typedefs altogether, they are unnecessary and bad style in C++.
The real solution is to just use named structs:
struct foo; // forward declaration
struct foo {
// … implementation
};
The typedefs are not useful.