Can I call a struct from within itself? [duplicate] - c++

This question already has answers here:
self referential struct definition?
(9 answers)
Closed 6 years ago.
I want to know if its possible to make this in C++:
struct Hello
{
Hello A, B;
};
I would appreciate any kind of help

No because such usage will consume an infinite amount of memory.
You can store pointers to the struct instead, and this is a common way, for example, in implementing a linked list.
struct Hello
{
Hello *A, *B;
};

Related

How to declare/define constructor with private data that is of a pre filled array [duplicate]

This question already has answers here:
Initializing a member array in constructor initializer
(8 answers)
How do I initialize a member array with an initializer_list?
(8 answers)
How can i use member initialization list to initialize an array?
(2 answers)
Closed 6 months ago.
so I am having an issue with defining and declaring my constructor, normally this isnt a problem but this time I the private data is 2 sets of pre filled arrays. And I am not sure of the syntax for doing this as it doesnt seem to follow the normal way for say.
Below is the relevant parts of the code.
class Frequency_Values{
public:
Frequency_Values(unsigned short LSB_LUT_[], unsigned short MSB_LUT_[]);
Frequency_Values(const Frequency_Values& F);
Frequency_Values& operator = (const Frequency_Values& F);
~Frequency_Values(){};
private:
unsigned short MSB_LUT[410] = {1,2,3}; //not the same data, its cut down cause the actual array is large and would make it messy here
unsigned short LSB_LUT[410] = {1,2,3};
//------------------------Constructor definition-----------------------------------------
Frequency_Values::Frequency_Values(unsigned short LSB_LUT_[],unsigned short MSB_LUT_[]):LSB_LUT[](LSB_LUT_[]),MSB_LUT[](MSB_LUT_[]){}
//---------------------------------------------------------------------------------------
The way I have it gives the following errors,
Any help with this would be greatly appreciated.
Thanks, Dean.

Is it legal to access class members as an array? [duplicate]

This question already has answers here:
Can I treat a struct like an array?
(8 answers)
Is it legal to index into a struct?
(10 answers)
Closed 2 years ago.
Consider this code:
struct Point {
double x;
double y;
double z;
void setByIndex(int i, double value) {
(&x)[i] = value;
}
};
The setByIndex() function works and does what I want. But can I be sure this is not a UB (when called with a proper i)? Could you please provide references to the standard with the explanation?

Different ways of declaring a function in C++ [duplicate]

This question already has answers here:
How does this template magic determine array parameter size?
(3 answers)
Can someone explain this template code that gives me the size of an array? [duplicate]
(4 answers)
How does this "size of array" template function work? [duplicate]
(1 answer)
Closed 2 years ago.
I have come across the following code snippet in C++ and I have no idea what it means;
#include <iostream>
char (&some_function(int (&some_input)));
int main ()
{
// main code here
return 0;
}
How is some_function a function? what kind of syntax is this? where is the body?
I'm sure this is only a C++ syntax as it doesn't compile in C.
Edit:
Actual code with above style:
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))

What is the difference between struct node* node and struct node *node? [duplicate]

This question already has an answer here:
What is the difference between (int *i) and (int* i) in context of both C and C++? [duplicate]
(1 answer)
Closed 7 years ago.
I am new to pointers and I don't know this. Please guide.
Thanks in advance.
There is absolutely no semantic difference, it's a purely stylistic choice.

C++: defining a struct inside another struct? [duplicate]

This question already has answers here:
Why would one use nested classes in C++?
(6 answers)
Closed 7 years ago.
so I've been playing around with C++. I was trying to figure out if it's possible to define a structure in the definition of another structure. And here's my code.
#include <iostream>
using namespace std;
int main(){
struct structure1{
int integer1;
struct structure2{
int integer2;
}struct2;
}struct1;
structure1 *s1 = &struct1;
s1->integer1 = 50;
cout<<"STRUCTURE ONE'S INTEGER: "<<s1->integer1<<endl;
cout<<"STRUCTURE ONE'S STRUCTURE2.integer2: "<<s1->struct2.integer2;
}
OUTPUT:
$ ./a.out
STRUCTURE ONE'S INTEGER: 50
STRUCTURE ONE'S STRUCTURE2.integer2: 0
From what I saw in the output it had seemed to be working. But I just don't understand why or how it worked. Is it good practice? Is there any application to this?
Thanks!
But I just don't understand why or how it worked.
Why wouldn't it work? It's perfectly legal C++.
Is it good practice?
Depends on how it's used.
Is there any application to this?
Definitely. For example when you only need structure2 inside structure1 and nowhere else. It's good to make its scope as small as possible.
For more information: Why would one use nested classes in C++?