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++?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I got an array at the end of my class and i don't know how to use it.
the bus[10] is so hard to understand. I don't know why it can access driver and what does empty() function really do.
#include "conio.h"
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "graphics.h"
#include "stdlib.h"
#include "dos.h"
static int p=0;
class a
{
char driver[10];// driver
public:
void install();// for installing
}bus[10];//here we declare the number of buses we can have.
void a::install()
{
cout<<"Enter bus no: ";//ques
cin >> bus[p].driver;// what does this mean
bus[p].empty();//what does this mean
p++;
}
This is syntax for defining a type, and an instance of that type, at the same time.
For example:
struct Foo {} foo;
is the same as:
struct Foo {};
Foo foo;
So your example defines the type a, and also creates an array of 10 as called bus.
It would be more clearly written thus:
class a
{
char driver[10];
public:
void install();
};
a bus[10];
In this manner we can now more easily see that you've created a global array called bus, which you can use like you'd use any other array.
Since p is zero (to begin with), bus[p] just gives you the "first" a object in the array (to begin with). As p is increased, subsequent buses are accessed.
So, this:
cin >> bus[p].driver;
reads into the driver member of the pth bus.*
And this:
bus[p].empty();
means nothing, because a does not have a member function called empty().
* Well, the p+1th bus, because array indices begin at zero but English doesn't!
P.S. You can do funny (read: stupid) things with this syntax!
This is a very strange looking code, probably from an old workbook.
I could help you with achieving the action that you want, but it's hard to understand anything from this snippet.
Wrong: As far as I remember adding a identifier at the end of an unnamed struct gave it a name just like the usual approach.
struct {
float x, y;
} Point;
//is equal to
struct Point {
float x, y;
}
However I'm not familiar with the array syntax you provided.
I suppose std::cin >> bus[p].driver is meant to read the "name" that the char[10] driver field is. But using a char array here is troublesome and it's much better to replace it with std::string and shortening it to 10 characters after input.
The empty() method is often used as a container function returning boolean and telling the programmer whether the container is empty or not. Here however this function is undeclared and the code won't compile either way.
Not to mention that non-const variables placed out of function scope, like the static int p = 0, are a grave mistake.
Not true: In conclusion this is a very messy code and without the knowledge of what you want to achieve nobody could help you here.
See the answer below for better explanation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Yes I know using namespace std is bad practice, but I have already written a majority of the code with this declaration in place, and I don't think I have the time to go back and modify it.
The reason for the global variable is that I'm using multiple threads which need access to this variable and need to modify it.
My question is, I have int remainder = 0; declared globally, and within my master thread I call remainder = 13 % 5; for example.
This gives me an error saying 'int remainder' redeclared as a different kind of symbol and I've read that the reason is that using namespace std overrides the std::modulus operator, if I understood that correctly.
What other methods can I use to perform this function, keeping using namespace std and remainder as a global variable?
#include<iostream>
#include<cmath>
using namespace std;
int remainder = 0;
void testing();
int main(){
testing();
cout << remainder << endl;
return 0;
}
void testing(){
remainder = 13 % 5;
}
The problem is your global variable name conflicts with std::remainder from the standard library. Example on Compiler Explorer.
The problem with using namespace std; is that it brings so many symbols into the global namespace that this error is almost inevitable. It's a bad practice for anything but the simplest toy programs.
The conflict is with std::remainder, not with %. The variable name you've chosen conflicts with a function in the std namespace. You already know using namespace std; is bad, so I'll spare you.
Options:
Lose the using statement.
Rename the remainder variable.
Put the remainder variable in its own namespace, and explicitly refer to it through that namespace.
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:
How to use a string as a variable name in C++? [duplicate]
(2 answers)
Closed 7 years ago.
Say I have the following struct:
struct movie {
char movie_name[32];
int rating;
int release_year;
char location;
}
Normally, I would access the rating by saying "movie.rating".
For this project, I have to take input from a text file. I will read a variable such as "movie_name" or "rating" or "release_year" from the file, and given that variable, I have to access the corresponding element of the struct.
Ex: if the input file reads "movie_name", then I want to access movie.movie_name. How do I do this without making 4 if statements? Is there another way?
if(input == "movie_name")
movie.movie_name = ...
else if(input == "rating")
movie.rating = ...
The real struct I'm working with has 20+ members, so I am trying to find a more efficient way to write this code.
Thanks in advance!
In C/C++ it is not possible to access variable using a string; so there is no way to do this using the struct you provide. A map might be an alternative:
map<string, int>
but then each variable will map on the same type of variable (int in this case)... You should look to the related questions: How to use a string as a variable name in C++? and Convert string to variable name or variable type
What you are looking for is called reflection. unfortunately, it is not supported in C++. One solution to your problem which is not an optimal one of course is to implement your struct as map of pair<key,value> as follow:
struct movie {
std::map<string,ValueType> foo;
}
However, the problem is the ValueType. If boost is available, Then this could be a better solution:
struct movie {
std::map<string,boost::variant<typeX, typeY>> foo;
}
This question already has answers here:
Define bitset size at initialization?
(7 answers)
Closed 5 years ago.
Now I've this code:-
The code is about taking in an integer and providing its binary form in the given number of bits.
#include <iostream>
#include <bitset>
using namespace std;
int main(){
//creating instance using bitset (6 bit). here you can specify the length such as 8,16,32,64...
int n=5;
bitset< 6 > btFlaged;
//assigning integer value to instance
btFlaged = 7;
//print bit string in the string
for(int i=btFlaged.size()-1;i>-1;i--)
{
cout <<btFlaged.test(i);
}
}
How do I use an integer(E.g. n) in the place of '6' so that a value entered at run-time can be used in the code?
I've done some research on the net and I know that bitset needs a value at compile time and so instead of bitset, I should use vector bool but I don't know how I should incorporate that in the program?
If any of you guys can tell me how to use vector or if you have an altogether different theory on how to the the task done, please do share.
Also I cant use boost:dynamic_bitset as the code is going to be judged by an online judge which may not have the seperate header file.
a std::bitset's size must be set at compile time as it is a template parameter. If you need a dynamic bitset you can look at boost:dynamic_bitset