use a pointer and be able to use class member functions [duplicate] - c++

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
So I have a pointer SparseMatrix *mat which holds values of the sparsematrix in a triplet format from a file. After I get the values, I would like to be able to just print it out using a print function I created in the SparseMatrix() class. However, I realize that this works if it wasn't a pointer and just 'mat'. I feel like this is something simple but I just cannot wrap my head around it. Any suggestions? Forgive me if this is an extremely noob question. Also, the reason I'm even using a pointer in the first place is because I am trying to visualize a sparsematrix using QT and my professor had given the basic backbone of the code. In that code, a private variable, SparseMatrix *mat was given.

Via pointers you can access your functions using ->.

Related

please explain this-> in c++ [duplicate]

This question already has answers here:
What is the 'this' pointer?
(8 answers)
Closed 5 years ago.
so I saw this syntax
this->
used in a c++ tutorial on udemy but it was not explained to me as to what exactly why I would use this in programming. Can anyone provide an example program or proper example code that shows what this does? Please explain the logic behind it. Please don't say Google it. I know it has something to do with pointers and memory. and what not I guess another thing I am looking for is an explanation a five year old would understand or broken down enough for easy comprehension.
this keyword is used to reference current instance of given class, for example:
class A {
public:
void setName(std::string name) {
// if you would use name variable directly it
// will refer to the function parameter,
//hence to refer the field of the class you need to use this
this->name = name;
}
private:
std::string namel
}

how to call functions from header file [duplicate]

This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 5 years ago.
CWmcaIntf::CWmcaIntf() :
m_hDll(NULL),
m_pLoad(NULL), m_pFree(NULL), m_pSetServer(NULL), m_pSetPort(NULL), m_pIsConnected(NULL),
m_pConnect(NULL), m_pDisconnect(NULL), m_pTransact(NULL), m_pQuery(NULL), m_pRequest(NULL), m_pAttach(NULL),
m_pDetach(NULL), m_pDetachWindow(NULL), m_pDetachAll(NULL), m_pSetOption(NULL),
m_pSetAccountIndexPwd(NULL), m_pSetOrderPwd(NULL), m_pSetHashPwd(NULL), m_pSetAccountNoPwd(NULL), m_pSetAccountNoByIndex(NULL)
i dont know what this grammer means. i am trying to use a header file 'CWamInf'. and want to know different methods or problems .. thanks..
I don't know how much you know about headers classes and stuff so I'll try to explain everything I can.
CWmcaIntf::CWmcaIntf() :
The CWmcaIntf:: means that the function that is about to be defined after the double colon is within the class "CWmcaIntf".
The CWmcaIntf() is a constructor : it has the same name as the class and its purpose is to construct each object of a class depending on the code you put in it. I'd say that often, its only purpose is to initialize member variables. If it isn't clear yet, check this link.
m_hDLL(NULL)
As you most certainly know, programmers tend to respect and harmonize on coding conventions. One of those is to name all member variables by m_something, so your code become easier to understand for you and everyone else.
So here, all you got is a constructor initializing a lot of member variables to NULL, which means that when a CWmcaIntf object will be constructed, its member variables will be equal to nothing and only be modifiable with setters if data are properly encapsulated.
Now if the purpose of your question was for us to tell you more about what does mean the member variables, and what the constructor may do, well don't expect an answer since zero details ae given and nothing even matches the tutorial explaining how to properly ask a question.

Does Python have a pre-existing class called 'object'? [duplicate]

This question already has answers here:
Why do Python classes inherit object?
(6 answers)
Closed 6 years ago.
I am learning to make classes using template codes and one of them is coded with what looks like inheritance syntax:
class KNNLearner(object):
What is the purpose of (object) here?
In python everything is an object, an object is an object. You can also create Meta-classes to change some behavior of your objects/object to implement something.
In you case KNNLearner(object) the (object) permits you to pass the class that you want to KNNLearner to inherit.

Access Struct.key using Dynamic variable Creation [duplicate]

This question already has answers here:
Dynamic Variable Naming and Reference (ColdFusion)
(2 answers)
Closed 7 years ago.
I have a struct called params inside are keys ch_1, ch_2, ch_3, etc
I am inside a loop that loops over query and use the id column to access the ch_(key) value
for(ck in checklist)
{
n_checklist.status = params.ch_(ck.key_value);
}
How would I write this?
I was thinking params.ch_#ck.key# would work but it doesn't. I want to create a property name using another variable value.
You can use associative array notation to accomplish this.
for (ck in checklist) {
n_checklist.status = params['ch_' & ck.key_value];
}

Accessing a class member variable by its name at runtime [duplicate]

This question already has answers here:
Get attribute by name
(5 answers)
Closed 4 years ago.
In the vein of more impossible-but-is-it-really questions:
Is it possible to access the member variable of a class, where the variable's name is stored in a string?
class Test
{
public:
int test = 0;
}
string name = "test"; // let's assume we know test is an int.
Any chance of getting the value of test, using the string?
One bit of cheating not allowed:
enum vartype {
INT,
..
}
No forcing the class to register all its variables in a std::map<string, std::pair<vartype, void*> >.
All other tricks welcome.
Thanks!
No.
To do this, you need to provide some mapping between member variables and the string names by which you intend to access them.
In the realm of really ugly kluges, you could build the program with debug information and have it use that to find the location of the variable in the same way a debugger would. But other than that, you're out of luck. C++ doesn't do reflection.
About why it's not available in C++ and an alternative: http://en.allexperts.com/q/C-1040/eval-function-javascript-C.htm
It's possible in MATLAB though...
As a very simple example, if you have a matrix updation to do, which goes like:
M1=1;
M2=2;
M3=3;
And you would prefer that the variable names could be altered so that you could use a for loop, then it can also be done this way:
for i=1:3
eval(['M' num2str(i) '=' num2str(i)]);
end
I used to do this in Actionscript. Was really glad to find that it's available in Matlab too