Method to change elements inside struct in C++? - c++

I have a structure with 2 fields which have a value. I would like to implement a method that changes the values of the fields. I did this with the following code, but it is not correct. I would like to ask how can I fix this?
#include <iostream>
using namespace std;
struct CPerson {
const char* name = "Ivan";
const char* gender = "Male";
};
void SetName(const char* szName) {
//what to write here?
*name = *szname;
}

A struct works like a class, the only difference is that all inside the struct is public by default.
So you can (and in this case you have to) declare setName inside the struct

Something like:
void SetName(CPerson& p, const char* szName) {
p.name = szName;
}
Be careful though, szName argument may be pointing to an array whose lifetime may end before the lifetime of CPerson::name ends. That makes CPerson::name an invalid pointer.

you need to make it a member function
#include <iostream>
using namespace std;
struct CPerson {
const char* name = "Ivan";
const char* gender = "Male";
void SetName(const char* szName) {
//what to write here?
name = szname;
}
};
that code is still wrong as its just juggling pointers. You should use std::string to store strings. But at least the code will compile;

I would like to implement a method that changes the values of the fields.
I wouldn't be so certain you should do that.
You see, the fields of a struct are, by default, publicly-accessible - which means anyone can change them, method or no method. Why bother writing such a method? Isn't it redundant?
If you had a class instead of a struct, or if you had set your field access to protected or private, then it makes sense to have a method for setting field value, e.g.:
#include <string>
#include <string_view>
struct Person {
protected:
std::string name = "Ivan";
std::string gender = "Male";
public:
void changeName(std::string_view new_name) {
name = new_name;
}
};
(string_view is new in C++17; before that, you could use const std::string&).
But even then it's not always a good idea to have that. Also, in this struct of yours, there's a default name and gender, without a good reason. Instead of both these things, you might make the struct immutable:
#include <string>
struct Person {
const std::string name;
const std::string gender;
};
which, for many applications, makes sense (e.g. if you get this from some database, rather than tracking a person's name and gender throughout their lives).

Related

How to store structure as (void *) and retrieve same

I am learning to use void pointers. Here I have created a resource class which can store any type of structure in (void *) and now when I try to get that structure back in (void *), I am not able to get same data back. Please help here
#include <iostream>
using namespace std;
class resource{
private:
void* data;
public:
void set(void* val){
data = val;
}
void get(void* val){
val = data;
}
};
class student{
struct st{
int id;
int age;
};
public:
void func(){
st *st1 = (st *)malloc(sizeof(st));
st1->id = 5;
st1->age = 10;
resource *rsrc = new resource();
rsrc->set((void*)st1);
void *data;
rsrc->get(&data);
st *st2 = (st*)data;
cout<<"get data %d"<<st2->id<<endl; // HERE I want to get same data back
}
};
int main() {
student *stu = new student();
stu->func();
return 0;
}
Just change the signature of get to return a void *:
void *get(){
return data;
}
And subsequently:
void *data = rsrc->get();
Also, it is idiomatic to use new, rather than malloc, to construct objects, although for POD (plain-old-data) types, either is valid.
Your get method won't return any value. You are passing a void pointer to it and inside the method, you overwrite that void pointer. However, that only overwrites the local copy of the void pointer and does not return a value. As others stated, you either have to pass a pointer to a pointer or use the return statement to return the value of the pointer.
In your specific case, #PaulSanders made the correct suggestion. You should use the getter/setter pattern. Also, he is correct that you should use new and delete in idomatic C++.
For user-created classes, most people specify classes with the first letter as a captial:
class Resource {};
class Student {};
It depends on your coding standard. It matters less whether you use camel case or not so long as you use a consistent naming convention.
Also, we generally try to prevent loss of type information in C++. Of course, you can use a C-style cast which will just reinterpret the pointer as the specified type but that is considered bad style and likely to cause problems.

How can I pass arguments to a class by creating an object in C++?

I am working on my first separate class file. I have been given a driver program which I am not supposed to change and I am to create a class file that runs off the driver.
#include <iostream>
#include <iomanip>
using namespace std;
#include "Question.h"
int main()
{
string q2Answers [] = {"China","India","Mexico","Australia"};
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
q2.display();
cout << endl;
}
The driver, simplified above, appears to be passing arguments to the class via arguments. My class header file is built in the following fashion.
#ifndef QUESTION_H
#define QUESTION_H
#include <string>
using namespace std;
class Question
{
public:
void setStem(string newStem);
void setAnswers(string newAnswers[]);
void setKey(char newKey);
void display();
string getStem();
string getAnswer(int index);
char getKey();
private:
string stem;
string answers[4];
char key;
};
#endif // QUESTION_H
How can I execute the functions in my class using arguments passed into an object? I am confused as to how the line,
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
has any way of pushing those arguments into the functions. Any insight into this would be very appreciated.
If I understood you correctly, you're asking for how to make a constructor(See OldProgrammer's link in the comment to your question):
You can either make it right in the header file, like so:
Question(const std::string& theQuestion,
const std::string theOptions[], const char& correctAnswer)
{
this->stem = theQuestion;
for(int i=0; i<4; i++){
this->answers[i] = theAnswers[i];
}
this->key = correctAnswer;
}
~Question(){}
//This is called the "Destructor", it is a function called when the object is destroyed
(You can imagine the const std::string&-part as string or const char&-part as just char if you don't know what they mean, since it's not very important right now.)
Or you can make it in the separate .cpp-file like so:
Question::Question(const std::string& theQuestion,
const std::string& theOptions[], const char& correctAnswer)
{
this->stem = theQuestion;
for(int i=0; i<4; i++){
this->answers[i] = theAnswers[i];
}
this->key = correctAnswer;
}
Question::~Question(){}
You might be asking why we use destructors; it's because sometimes there are things we need to do before the object is removed.
Like for instance if you want to save certain info or make a change, or more commonly to free dynamic memory you allocated when you made the object. Otherwise you get memory leaks, which are bad.
Then you can construct/create an object as such:
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
You can also "overload" the constructor, i.e. you can make other versions of it. For example if you imagine that the constructor's only parameter was the question:
Question(std::string q){
this->stem = q;
}
Question(char c[]){
this->stem = c;
}
Now you can pass either a string or an array of characters to the object. But if you only have one, you can't do the other too, so if we only have the first constructor, we can't pass an array of characters to do the exact same thing. And you can make as many of these as you like, but it doesn't necessarily mean that it's better just because it has a ton of constructors.

I am trying to make a hashmap of of string functions

I am trying to make a map which stores a string as an identifier and a function that returns a string i have tried typedef but i kept running into problems because i couldn't convert my typedef string (command)() to a regular string i have also tried map commands but it gives me an expression expected error but it does work if i replace string with int. Does anybody know a way of doing this? This is what my code looks like
#include "iostream"
#include <map>
#include <functional>
using namespace std;
class GameController {
public:
void inputReader();
private:
bool gameOver = false;
map<string,string(*)()> commands;//Does not work
//commands
string commandReader(string* inputCommand);
void initCommands();
//both
char* end();
string run();
//while attacking
string attack();
string usePotion();
string useItem();
//while in room
string engage();
string searchRoom();
string rest();
string checkBag();
string checkMap();
string checkStats();
//string save();
};
#endif //ROGUE_GAMECONTROLLER_H
#include "GameController.h"
GameController::GameController(){
initCommands();
}
void GameController::inputReader() {
while (!gameOver){
string x;
getline(cin,x);
cout << commandReader(&x) << endl;
}
}
string GameController::commandReader(string *inputCommand) {
for (map<string,string>::iterator it = commands.begin(); it!=commands.end(); ++it)
{
if(it->first == *inputCommand)
{
return it->second;
}
}
return "Computer says no type help for commands";
}
void GameController::initCommands() {
commands["end"] = end;
//while attacking
commands["run"] = run;
commands["attack"] = attack;
commands["use potion"] = usePotion;
commands["use item"] = useItem;
//while in room
commands["engage"] = engage;//TODO
commands["search"] = searchRoom;
commands["rest"] = rest;
commands["check bag"] = checkBag;
commands["map"] = checkMap;
commands["stats"] = checkStats;
}
This question is tagged C++11, so here's a concise example which uses unordered_map (a real hash map, unlike std::map which my STL reference says is commonly implemented using binary search trees), and std::function.
#include <iostream>
#include <functional>
#include <string>
#include <unordered_map>
std::string foo()
{
return "foo!";
}
struct MyClass
{
static std::string bar()
{ return "bar!"; }
std::string FizzBuzz() const
{ return "FizzBuzz!"; }
std::string operator()() const
{ return "Myclass!"; }
};
int main(int argc, char **argv)
{
MyClass mc;
std::unordered_map<std::string, std::function<std::string()>> commands;
commands["myfoo"] = foo;
commands["mybar"] = MyClass::bar;
commands["myfb"] = std::bind(&MyClass::FizzBuzz, mc);
commands["myclass"] = mc;
for( const auto &f : commands)
std::cout << f.second() << std::endl;
std::cout << commands["myfoo"]() << std::endl;
return 0;
}
Pointers to member functions is not like pointers to free functions or even static methods. For one thing all member functions have a hidden this pointer in the function parameters that makes all of this object magic work.
Going through step by step:
First, define a helper:
typedef string (GameController::*funcp)();
This defines type funcp which represents a pointer to a member function of GameController (to partly take care of the this problem) that takes no parameters and returns string
Then, modify your map to use funcp
map<string, funcp> commands;
Then you have to change the assignment of the member functions a bit to be brutally explicit that it is a pointer and a member of GameController
commands["end"] = &GameController::end;
You can also save yourself some runtime trouble and use an initializer list here rather than a function and a map in every single GameController object. That'll take a bit of extra explaining and I have to be on the move in a few minutes. Sorry about that. A static map with static initializing really is better and worth your time researching, though.
The next bit I stole from the C++ Super FAQ. Read this link. Worth reading all of it, because it heads off a lot of the question you will have.
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
This makes calling the function awesomely easy.
return CALL_MEMBER_FN(*this, it->second)();
And that should about do it for you.
edit:
Tweej demonstrates the generally better way to do this, std::function and std::bind, in their answer. Since I'm advocating the ancient ways, I'd like to explain why.
Two reasons: one is tunnel vision directly answering OP's question.
The second is With the ancient ways I could easily make commands static and save having to create a new copy of commands for every instance of GameController. When using std::bind, you have to have the bound object, and that ruins the static idea.
Poking around at the idea of just using std::function seems to have born fruit and rendered obsolete the ancient ways. gone is the CALL_MEMBER_FN macro. Gone is the funcp typedef
The map is now defined as static, what I was aiming for the the old-pre C++11 approach. Note the funcp typedef is replaced by a function that takes a pointer to GameController to supply this.
static map<string, std::function<string(GameController*)>> commands;
And the map is now rigged to use a static initializer list. No function required. This initializer needs to sit outside the class definition because... I'm not sure why. I think this is changed in C++14.
map<string, std::function<string(GameController*)>> GameController::commands
{
{"end", &GameController::end},
{"run", &GameController::run},
{"attack", &GameController::attack},
{"use potion", &GameController::usePotion},
{"use item", &GameController::useItem},
{"engage", &GameController::engage},
{"search", &GameController::searchRoom},
{"rest", &GameController::rest},
{"check bag", &GameController::checkBag},
{"map", &GameController::checkMap},
{"stats", &GameController::checkStats}
};
The map is initialized once and only once. All GameControllers will use the same commands, so the constructor is really dumb
GameController::GameController()
{
// init function is gone
}
Command reader gets a big rip-up, mostly because the point of a map is you can search it by the key. So I search for the key rather than iterating. The function call is now obvious and dead simple:
string GameController::commandReader(const string &inputCommand)
{
map<string, std::function<string(GameController*)>>::iterator found = commands.find(inputCommand);
if (found != commands.end())
{
return found->second(this);
}
return "Computer says no type help for commands";
}

Copy string value into a class field?

I'm new to and learning C++. I know a fair amount of Java and some C.
What I want to do is to create an immutable name class that takes in a string value, copies that string to a class field and then eventually hashes it to an ID that can be parsed much more efficiently than a string.
I'm hitting a wall due to a general lack of knowledge of C++ strings. Here's what I have so far...
#pragma once
#include <string>
class Name
{
public:
Name(std::string s);
~Name(void);
int getId();
std::string getName();
private:
int id;
std::string name;
};
and...
#include "Name.h"
Name::Name(std::string s)
{
}
So what I want to do is store the value of s, passed in by the constructor in the "name" private field. As far as I know a new string object must be created and then the value of s must be copied into it.
I also think that the argument s can and should be a string pointer instead of a string object (to prevent an unnecessary copy from occurring). If I'm right then the constructor should look like the following, right?
Name::Name(std::string &s) { ... }
In this case, nothing would need to be done special when passing in a name? IE.
Name n = new Name("Cody");
is perfectly valid? Actually I'm not sure since "Cody" to my knowledge is a constant string or something like that.
So if I'm all on the right track, then what is the proper way to actually copy the value? I'm thinking this is appropriate but I'm not sure.
#include "Name.h"
Name::Name(std::string s)
{
name = new string(s);
}
Thanks for the help in advance, I know it's a basic question but I'm slowly making baby steps into the C++ world. :) - Cody
You are close, your code can be like this after a little massage:
class Name
{
public:
Name(const std::string& s); // add const and reference
~Name(void);
int getId() cosnt; // add const
std::string getName() const; // add const
private:
int id;
std::string name;
};
Name.cpp
Name::Name(const std::string& s):name(s)
{
}
Here :name(s) is called member initializer list.
Name n = new Name("Cody"); is perfectly valid? Actually I'm not sure
since "Cody" to my knowledge is a constant string or something like
that.
No, n is not pointer, it's not like java you need to new for every object. In C++, you do
Name n("Cody");
This will call Name(const std::string& s) to initialize object n and initialize name string with "Cody".
Note: variable n has automatic storage duration, it will be destroyed if it goes out of scope.
To let n on dynamic storage duration, you need to use new/delete pair:
Name *pn = new Name("Cody");
delete pn;
or use smart pointers, you no need to call delete n_ptr; as n_ptr will be destroyed when it goes out of scope as well:
#include <memory>
std::shared_ptr<Name> n_ptr(new Name("Cody"));
EDIT:
To use Name class in other classes, it's the same way when you use string in Name class, you don't have to use pointers.
class TestName
{
public:
TestName(const Name& n):name_(n){ }
private:
Name name_;
};
TestName tn("Cody");
You should use a constant reference to std::string here.
As you said, it would prevent unnecessary copies.. But then why not just a pointer or a constant pointer?
A constant reference would allow you to pass to your function some arguments that would implicitly call the right std::string constructor.
So, in a nutshell, you could do that:
Name::Name(const std::string& s)
{
this->name = s;
}
// Or even better..
Name::Name(const std::string& s):
name(s)
{
}
int main(void)
{
Name nick("hello");
return 0;
}
You can find out about every std::string's constructors on its cplusplus.com's sheet.

char array compilation error

What is the problem in my code? It does not compile..
class FileNames
{
public:
static char* dir;
static char name[100];
static void Init3D()
{
FileNames::dir = "C://3D//";
FileNames::name = "abc";
}
};
You cannot assign to an array, so FileNames::name = "abc" fails (char arr[4] = "abc" works, however, because this is a direct initialization, not assignment). Either use a char* here as well, or use strcpy to copy data to the array or better a std::string which avoids many of the downsides of raw strings.
Most importantly you need to define your static members somewhere at global scope, outside a function:
char FileNames::name[100];. At this time initialization syntax using = would be possible even with the array, but the string to be assigned needs to have the same length as the array.
There are two problems with your code:
1) You've duplicated the variable name (with two different types).
2) You can't initialise static members like that (see example below).
The last thing isn't a problem as such, but you should consider using std::string instead, as that encapsulates string functionality so that you don't need to deal with raw pointers. It's a lot less painful, especially if you're new to this sort of thing.
Change it to this:
// Header file
class FileNames
{
private:
static char* name;
public:
static char* dir;
};
CPP file
#include "FileNames.h"
char* FileNames::name = "abc";
char* FileNames::dir = "C://3D//";
// Now use your class...
Try initializing like this:
class FileNames
{
public:
static char* dir;
static char name[];
};
char *FileNames::dir = "C://3D//";
char FileNames::name[100] = "abc";
Did you remember to actually define the static members outside of the class? Also, I don't believe you need to resolve the scope unless you're actually outside of the class.
Use:
FileNames::dir = new char[strlen("C://3D//")];
strcpy(FileNames::dir, "C://3D//");
strcpy(FileNames::name, "abc");
Also, don't forget to #include <cstring> and to later delete[](FileNames::dir)