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.
Related
This question already has answers here:
C/C++ Struct vs Class
(7 answers)
Closed last year.
Is memory allocation for class (when initializing an object) is the same as struct? I know that when we initialize struct,
struct example {
int a;
double b;
int c;
}
the memory allocated will be = (0-4) int, (8-16) double, (16-20) int. (If I am not mistaken)
What if we want to initialize
class Example {
private:
int a;
double b;
int c;
public:
Example();
}
Example object();
?
What's the memory allocation behavior when the object is initialized? Is it the same as struct?
Yes. Literally the only difference between a class and a struct is that in a struct, member-variables/methods listed in the first section of the declaration (above any public:/protected:/private: lines) are treated as public, whereas in a class they are treated as private.
Ask sizeof. It is always correct. In this simple case, the class is guaranteed to have no hidden members; in general that isn't true.
The rule isn't about class vs struct; the only difference is the default in struct is public members but class is private members. The rule is about when you use expensive things. When you add virtual in, the sizes of things start to change. When you add virtual inheritance, things get a lot bigger.
You can only use RTTI on a class that has a virtual member. See How to get the typeid of a void* pointer? for details.
You have asked a further question; whether you can just add up the sizes of the members. The answer is a definite "No". You can't do that. On my platform, that struct may well take up, 24 bytes due to alignment (the double wants to be 8 byte aligned).
This question already has answers here:
Order of elements in set of pointers
(4 answers)
c++ custom compare function for std::sort()
(3 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I want to declare set of std::unique_ptr<A> like this:
class A
{
public:
///type of itself
typedef A self;
///Simple constructor
A(int w) : weight(w) {}
///To use for sorting
bool operator<(const self& another)
{
return weight < another.weight;
}
private:
int weight;
};//class A
//Container of pointers to A sorted by object, not by pointer values.
std::set(std::unique_ptr<A>, /*something*/) storage_for_A;
So, what to use in something? The obvious way is to declare a class for comparing two std::unique_ptr<A> by value of A and use it in above container declaration. However is there a better way? May be something predefined exists already? Surely, the problem is pretty standard.
Update: I know there is a question "Order of elements in set of pointers", but this is not what I am asking here. I know how set of pointers works. And I know about the solution, suggested there, which is mentioned in my question. I explicitly asked whether solution without custom-defined class for comparison is possible. E.g. through use of lambda functions?
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 2 years ago.
When writing C++ code, what is the difference between using class and struct?
class Foo final { };
vs.
struct Bar final { };
When should I prefer one over the other? Is this merely a choice of programming/coding style?
A struct simply defines the layout of a piece of storage.
A class is a dynamically-allocated "thing" which can have methods ... it can do things.
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.
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;
};