This question already has an answer here:
Why are redundant scope qualifications supported by the compiler, and is it legal?
(1 answer)
Closed 8 years ago.
What does it mean if I write:
struct Str1 {
char val1;
};
struct Str2 {
struct Str1::Str1 valS;
};
what does the added qualification ::Str1 mean?
Bonus points: How to match this one with a clang ASTMatcher ;-)
Greetings
It's the same as:
Str1 valS;
the extra struct Str1:: is redundant. See N0444
Related
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?
This question already has answers here:
Questions regarding C++ non-POD unions
(3 answers)
Closed 2 years ago.
#include<iostream>
using namespace std;
union f
{
string car;
int model;
};
int main(){
union f p;
p.car = "mclaren";
cout<<endl<<"car :"<<p.car<<endl;
return 0;
}
"string car;" gives an error an something like this " is implicitly deleted because the default definition would be ill-formed:".
but when i replaced the string with a char it showed no error. is this becuase unions dont work with string datatypes or there is some other reason?
Since the std::string is non-POD, you will have to define a constructor/destructor to make it work for c++11 and above.
This question already has answers here:
Undefined reference to static variable c++
(3 answers)
Closed 6 years ago.
I am trying to implement an answer given on my question over at CodeReview.SE. Basically, I want to access some static variables in a templated struct. Consider the following example code:
#include <iostream>
using namespace std;
template<const int idx>
struct Data{
static int bar;
};
template<const int idx>
int getBar(){
return Data<idx>::bar;
}
int main() {
const int n = 2; // Arbitrary number
cout << getBar<n>();
return 0;
}
The compiler does not recognize that I want Data<n> to be available in the program - however, it recognizes the initial getBar<n> function just fine as is evident from the error message:
undefined reference to `Data<2>::bar'
How do I tell the compiler to make the templated struct available as well?
Static class variables must be given memory allocation. Add this:
template<const int idx>
int Data<idx>::bar = 0;
Demo
Edit: The dupe linked by NathanOliver hits it on the head, but for non-templated classes. This answer shows the syntax when the class is templated. minor difference, but still useful.
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;
};
This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 9 years ago.
class Test
{
struct
{
unsigned has_some_value1: 1;
unsigned has_some_value2: 1;
} info;
};
What does unsigned has_some_value1: 1; means?
Should be the following statement true: sizoef(type) == bit1 + ... + bitn ?
These are called "bit fields". has_some_value1 occupies one bit. has_some_value2 also occupies one bit—maybe the next physical bit in memory, or maybe not (depends how your compiler is configured to handle bit field alignment).
A bitfield in a nonstatic instance of an un-named struct called "info", which is itself a member of "Test".