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.
Related
This question already has answers here:
Why use a const member function?
(3 answers)
What are the semantics of a const member function?
(9 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Edit: From #Jeremy Friesner's answer, he provides another view point that Why use a const member function & semantics of a const member function don't cover. It can improve clarity and disambiguous your code. People can have a brief idea of what this function is supposed to do or not do at the first glance. Very useful especially in working with others.
I come across the constant function lately.
It says:
Constant functions are those have denied permission when attempting
to change the member variables of their classes.
My questions are
In a programmer perspective, if you don't want a function being able to change member
variables, why not just moving the parts responsible for changing variables outside
of the function at the first place? (e.g using another function)
What/when are some practical moments of having the neediness to use a constant
function?
Appreciate any answers
class Foo
{
public:
int x = 0;
int Bar(int arg) const
{
x++; // fails
return x; // okay
}
};
The purpose of const-tagged methods isn't to prevent the programmer from intentionally modifying member variables, but rather to allow the compiler to assist the programmer by producing a compile-time error when the programmer accidentally modifies a member-variable. This is useful because it's much easier and quicker to correct a compile-time error than to chase down a run-time misbehavior through manual testing.
The const tag also assists later programmers who are reading the class's header file by helping them quickly understand what a method does or does not do. For example, if you see this in a class declaration:
class MyClass
{
[...]
int CalculateTheValue();
... you might ask yourself, "does calling CalculateTheValue() change the state of the MyClass object?" With a name like CalculateTheValue() it seems like it shouldn't, but there's no way to know for sure without finding the corresponding .cpp file and reading through the code that implements that method... which is a tedious and error-prone way to do things.
On the other hand, if you see this:
class MyClass
{
[...]
int CalculateTheValue() const;
... then you know right away that CalculateTheValue() will not modify the MyClass object you call it on, because it says so in the declaration. (if it did try to modify the MyClass object, the code wouldn't compile; so if the code compiled, we know it doesn't modify it)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First thing, I am not looking for a tool that is going to produce an unmaintainable C code from an existing cpp code-base. It is a requirement from a client, so I cannot just use the CPP code base itself.
I am trying to work out a work-flow, so that I can convert the code-base from CPP to C incrementally without breaking the code. I have thought out a few things using the "extern C" approach
Classes to Structs
Member functions of the Classes will be converted to Struct_name_FunctionName format.
In case of a function being re-used by 2 classes, I plan to use function pointers.
Replace overloaded operators like +,- etc with actual functions. Eg a Add_, Sub_ etc
Can you folks add anymore ?
and point out potential pit-falls ?
I cannot share the code-base because of NDA.
The code base isn't huge in itself. Its got around 50 cpp files, saw some 100 odd classes.
Thanks in advance.
P.s I have gone through some other questions that sound similar. But they don't really have the work-flow that I am looking for.
[Closing] : Although the post has been put on hold because it is too broad, many of the comments were in fact useful. I am clearer on what needs to be done than before. Thank you.
Well, there's a very long list you'll have to tackle. Without claiming to be complete, you also need:
Virtual function support (not impossibly hard, function pointer table)
Constructors and destructors (Reasonable mapping to ordinary functions, lot of work)
Exceptions and stack unwinding (extremely hard)
Missing C++ library functions and classes (lot of work)
But don't kid yourself. std::map<std::string, std::pair<int, int>> is a very straightforward class that stores two ints for each string. The C translation of that class is an outright mess. MyMap["five,three"] = std::make_pair(5,3) can easily become 100+ lines of C code.
If your C++ code makes extensive use of OO constructs - particularly inheritance and polymorphism - you might wish to look at some C libraries that mimic this in C. qobject (from the qemu source tree) is one I know well, but better known (to nearly everyone bar me) is http://en.wikipedia.org/wiki/GObject which comes from glib. Nowadays, that isn't tied up with GUI code.
The advantage here is that you can change the language in use without making too many changes to the program flow.
glib also provides a fair number of other useful library constructs.
Following the advice to take the GObject as an example how C++-like code is done in C, there's one thing you can try out:
Translate the C++ code to Vala
Generate C code from Vala compiler.
Vala is a C#-like language; except that you'll have to repeat "public" or "private" in every function signature, as well as get rid of pointer-star in class types in use, there's no much things you'll have to do.
Of course, the generated C code will look the same ugly as from the other generators, but at least there are some short ways how to make it a "maintainable" code (the main problem is that every object created in Vala results in incrementing reference counter in C code, which isn't always necessary).
The GObject model can be a nice instruction/example as to how to translate C++ constructs into the same in C. If you don't use exceptions (Vala does! :) ), there should be no trouble with that. Of course the main guidelines are:
Normal methods: use the pointer to object as first argument. The function name is NAMESPACE_CLASSNAME_METHODNAME.
Virtual methods: you have to create a "characteristic object", pointed by the "object" of that class, which contains pointers to functions. The class's characteristic objects are usually created in GObject in functions that return pointer to that object - the object itself is lazily created and saved in a static local variable.
Overloading: add name-distinguising parts (Vala does not support overloading, even in constructors - constructors use special syntax for calling named constructors)
Templates: expand in place :)
Derivation: single derivation only (as in Vala as well), make the first field of the "derived class" structure the field of type of the "base class" structure.
Calling methods from base classes: use C cast. If you follow the point 5, you should simply cast the structure of derived class object to the structure of the base class.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What's the difference between the following 4 styles of calling another member function?
Is there a benefit that causes that one method is preferrable?
void Object::trigger() {
(*this).triggerinner(10);
this->triggerinner(10);
triggerinner(10);
Object::triggerinner(10);
}
void Object::triggerinner(int x) {
std::cout << "trigger" << std::endl;
}
The first three are fundamentally the same in non-template code.
In a template member function, the first two make the name
lookup dependent (so the compiler can find names in a dependent
base class). Generally, simpler is better, so most people favor
the third form, except when the dependend lookup is necessary,
in which case they will use the second.
The fourth form blocks virtual resolution. (And it has, in
fact, the same three forms as above, i.e.
this->Object::triggerinner(10), etc.) You use it when you
want to force the resolution: the function called will be in
Object or in a base class of Object, but never in a derived
class, even if the function is virtual. This is most often used
in a derived class, to call the base class implementation before
(or after) doing additional work:
void
Derived::func()
{
Base::func();
// Additional work here...
}
Outside of a member function, it can also be used to call
a static member function when you don't have an object.
The first two are exactly equivalent to each other. By definition, (*p).thing and p->thing are equivalent for any pointer.
The third is equivalent in this situation; but could have a different meaning if the member function's name were hidden by a local declaration. There are also situations (where the function is a member of a base class, and templates are involved) when this form won't find the function, in which case you would have to use one of the others.
The fourth is equivalent if the function is not virtual. If it is virtual, then this will force a non-virtual call to the override available in this class, rather than the final override.
(*this).triggerinner(10);
It's ugly, "->" is there to avoid to do that.
this->triggerinner(10);
It's fine but this-> is not mandatory. If you are working on an existing project, keep the current coding style.
triggerinner(10);
It's fine, same comment than the solution using this->.
Object::triggerinner(10);
Use this for static methods.
I would suggest you to use triggerinner(10) since it's short and clear.
the most frequently used one is this->triggerinner(10);.The reason it makes an easy match using the pointer with table.
this->triggerinner(10) is better than the others:
Better than (*this).triggerinner(10) because it is more readable.
better than triggerinner(10), because in this case you will not know if triggerinner() is a method or a function (with a different scope then).
Better than Object::triggerinner(10) which is mainly used for static methods (but not only)
There is no difference, unless your compiler does some funky stuff. Try reading the assembly language generated by the lines in your function and see if there are any differences or if its the same block of instructions generated over and over.
As for the "best" or " most popular" one that comes down to your personal preference or when working at a company they might enforce certain coding standards forcing you to write in a specific way when doing something every time.
This question already has answers here:
Why use getters and setters/accessors?
(37 answers)
Closed 9 years ago.
I can't understand why people keep saying that when you make a class you should make a get() method to get the variable in a class, and a set() method to set a variable in a class.
If you have a get()/set() method... then why not just make the variable public in the first place?
It's not as if you would accidentally change it anyway since you still need to type myClassName.variable
Am I missing something?
Using get/set functions gives you flexibility to change your class implementation later without having to change all code which accesses the class.
For example, you might change the type of one of your internal variables, or you want to add restrictions on what values a variable can be set to. Perhaps you might want to only calculate a value on demand.
Also, you can make a get/set function non-inline, and put a breakpoint on it.
Making public variables is a very bad practice(in OO), because if you want to add any functionality or restrictions you will have to find all the code that accesses your class and replace that.
Getters and setters solve the issue of having to change all that code, when you want do add a restriction to a value of a variable, to do a validation or another functionality.
But don't overuse get/set functions, getters and setters also break OO principles, because they are still exposing implementation. If you really want your code to be reusable on different projects, use get/set functions only when there's no other possible implementation.
If you don't believe me, read this old but still relevant articles, and take your own conclusions.
http://www.idinews.com/quasiClass.pdf
http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
http://typicalprogrammer.com/?p=23
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