C++: meaning of std::ostringstream& var [duplicate] - c++

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.

Related

How i can get variable name in c++ without define [duplicate]

This question already has answers here:
converting a variable name to a string in C++
(8 answers)
Closed 2 years ago.
I'm looking for a function that returns a variable name
Theoretical example
template <typename Type>
std::string GetVarName(Type Var)
{
//Get Name
return Variable_name;
}
You could try to stringify the variable using the preprocessor. In the posted link by Schultke there are some examples using macro in order to achieve that.

Set variable value making reference to it by a string [duplicate]

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;
// ...
}

Convert int& to string [duplicate]

This question already has answers here:
Simple C++ - about strings and concatenation and converting int to string [duplicate]
(3 answers)
Closed 6 years ago.
This may seem trivial to normal C++ users, but I'm currently relearning C++ all over again. I have a function that takes in an int& and I would like to add that value to a string for a print statement. So, I have something like this:
std::string getTest(void* dataGroup, int& xWidth, int& yHeight)
{
std::string results = "";
results += "\nxWidth is: " + xWidth;
return results;
}
which fails to run when I call it. What is the proper way to convert the pass-by-reference int& value into a form that can be added to a string?
std::to_string(xWidth).
The & is pretty much irrelevant. to_string takes the int by value, so how your function takes it doesn't matter: by value, lvalue ref, rvalue ref, const or non-const... all will work.

Meaning of asterisk following a member function [duplicate]

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.

How to modify a string in a function? [duplicate]

This question already has answers here:
c++ change function's variable argument
(2 answers)
Closed 8 years ago.
Say I have
string stringInput = "hello";
alter(stringInput);
cout << stringInput;
and a function:
void alter(string stringIn){
stringIn[0] = stringIn[3];
}
Ideally I would want cout to produce "lello". But right now it simply returns "hello" as originally. I know this has something to do with addresses and pointers... how would I achieve this?
It's actually just because a new copy of the string is created for use in the function. To modify the string directly in the function add an & before the string name in the function header like this:
void alter(string &stringIn){
That passes the string by reference. Otherwise you could just return a string from the function.
All you need to do is to pass the string by reference:
void alter(string& stringIn){
// ^
stringIn[0] = stringIn[3];
}
You should also modify accordingly any function declarations you have for alter().
Your stringIn is a local variable. So when you pass it on the function as a value it just makes a new stringIn with different address. So the changes you are making in alter is only affecting the new stringIn. You need to recieve the reference of the stringIn in alter to make it work.
void alter(string& stringIn){
stringIn[0] = stringIn[3];
}