This question already has answers here:
What is the function of an asterisk before a function name?
(4 answers)
Closed 6 years ago.
I am fairly new to C++ and I am trying to decode the piece of code shown below. In particular for the BaseSetAssoc::BlkType* line, I am not sure what the asterisk means in this case. I would appreciate some insight.
BaseSetAssoc::BlkType*
NMRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id)
{
// Accesses are based on parent class, no need to do anything special
BlkType *blk = BaseSetAssoc::accessBlock(addr, is_secure, lat, master_id);
if (blk != NULL) {
// move this block to head of the MRU list
sets[blk->set].moveToHead(blk);
DPRINTF(CacheRepl, "set %x: moving blk %x (%s) to MRU\n",
blk->set, regenerateBlkAddr(blk->tag, blk->set),
is_secure ? "s" : "ns");
}
return blk;
}
BlkType isn't a member function, it's a type, possibly an enum or struct if not an inner class.
The BaseSetAssoc:: is needed to access such "inner" types (defined within a class, i.e. BaseSetAssoc).
So BaseSetAssoc::BlkType* is just a BaseSetAssoc::BlkType pointer.
It's not "following", it's "preceding". As the comments have said: it means that it is returning a pointer to BaseSetAssoc::BlkType, rather than a whole BaseSetAssoc::BlkType.
What does this mean? It means mostly that the pointer can be NULL, or non-existent. Before using the result of this function, it is almost mandatory that you check if it is NULL first.
Related
This question already has answers here:
What happens when you call data() on a std::vector<bool>?
(4 answers)
Closed last year.
I required your help for a very strange behaviour that I can't understand.
I wrote a simple usage of vector.data :
void* ptr = NULL; // really initialized somewhere else
bool* boolPtr = NULL;
boolPtr = ((std::vector<bool>*)ptr)->data();
and when I compile (with -std=c++17) I got the error
void value not ignored as it ought to be
I try a lot a things but it seems that each time I call, from a casted vector (from void*), the data() method return a void instead of a bool*.
What did I miss ?
vector<bool> is not a proper vector. As weird as that sounds, that's the way it is. It can't give you a pointer to its internal bool array, because it doesn't have one, since it stores the values packed into single bits.
This question already has answers here:
Convert string to variable name or variable type
(7 answers)
Closed 5 years ago.
If the title is not so suggestive I hope the explanation might help you understand my problem.
I have a function which sets some variables value. But the variables name I would like to provide it as a string, this way:
void setValue( const std:: string& variable_name, int value){ variable_name=value;}
then when I call this function like this:
setValue("variable", 10);
I will expect to execute set variable=10;
Any idea if this approach is possible or other ways I could have this behaviour?
Thanks!
It is not possible to magically retrieve a variable from a run-time string. You need to provide a mapping in advance, which is probably not what you want. E.g.
int& getFromName(const std::string& s)
{
if(s == "variable") return variable;
if(s == "foo") return foo;
// ...
}
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:
What is a reference variable in C++?
(12 answers)
Closed 7 years ago.
I want to implement a function with a parameter that will receive a string result:
bool function (entry parameter, std::ostringstream & var)
I'm not sure about what it is, pointer ?
Should I do something special about it or just: var << result ?
The return value is boolean but we will need var after
& means it is a reference, not a pointer. See Andrew's comment link. You do not need to delete var, if that is what you mean by "something special." Given the method signature you provided, the code:
var << result;
should work just fine, assuming "result" is something sane. If you need more information, you may need to post a MCVE.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to create a constructor that sets an array field to null.
Im getting a "no viable overloaded '=' " error
Horse::Horse()
{
string *ptr;
ptr = NULL;
Name[SIZE] = ptr;
}
My question is what exactly is going on behind the scenes here. I thought I could create a string pointer, set it to null, then set the array to the pointer and it would make the first element of the array equal to null?
Thanks
Setting Name[SIZE] to point to the address of std::string * is meaningless.
If Name is a std::string array, then the array will be defaulted empty.
If your task was to set the Name field empty in the constructor, I'm going to assume this is a const char* instead of a std::string. This can be done using initializer list:
Horse::Horse()
: Name{0} // Name{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} does the same thing.
{} // empty body
The above will initialize all the 20 positions to 0, or NULL if you like. Just as if you declared it in the global scope.
If you are not familiar with the initializer list in constructors, you can always do this, but at an extra cost:
Horse::Horse() // Initialize members runs first
{
for (int i = 0; i != SIZE; ++i) // Assign values to Name array
Name[i] = 0;
}
Side note: Instead of using macros to define your values, a better choice is to use const unsigned SIZE = 20; for reasons described here
I'll just go ahead and write the whole thing for you.
If you think about what a name is, it's just a string, a string of characters. So Name should be a std::string. Then the constructor would look like this:
Horse::Horse() {}
Done. Name will be initialized to an empty string, i.e. "".
If the assignment requires you to use C-strings (char arrays) instead of std::string, then Name should be of type char*, if it needs to be set to NULL. Now the constructor would look like this:
Horse::Horse() : Name(NULL) {}
The Name field will be initialized to NULL, i.e. it will be a null pointer, hence it doesn't contain even an empty string.