| C++ Pointers
||C++ PointersWhen a member function and variable name collide|
The standard practice / recommendation on how to handle a member function and variable name collision is to append an underscore '_' to the end of the variable name.|
This advice is troublesome when you are adding a mutator member function to an existing class and the variable is widely used throughout the class. Also the resulting code is untidy with some variable names having underscores and the remainder being without.
This "C++ pointer" shows you how to have member function variables and class variables with same name.
|Overview of the Example Programme|
To be easily understandable for everyone, the example code is based on a simple class for a square. In the initial version of the class the user can set the length of the square's sides and obtain the calculated area. In this version of the class (Square_v0.cpp") the member function variable name for the length of the square's side is length and the variable name is length_, as per the common recommendation.|
The standard practice means that a "later requirement" to add a mutator function to set the area of the square would require the variable area to be renamed to area_ throughout the class. However using this "C++ Pointer" means that the class variable area does not have to be renamed.
When the class was refractored to add the setArea() mutator member function the class variable
length_ is renamed to length using this "C++ Pointer"
Please note that this "C++ Pointer" is distributed with NO WARRANTY.
|How to have a class variable name the same as the member function variable name.|
The class variable name can be the same as the member function variable name as follows;|
void Square::setArea(double area)
this->area = area;
length = sqrt(area);
Refer to the file Square.cpp to view the above code in context.
|Software for Download and Installation|
The example programme for this "C++ Pointer" was compiled and tested using gcc version 4.4.1 running under Fedora 11. |
To obtain the example programme for this "C++ Pointer" use the following procedure.
|1.||Download and save to disk CSOCppPtr2.tar.gz (2.0kB)|
|2.||Start a console and navigate to the directory you saved the zipped tar file in. Extract the source with tar zxvf CSOCppPtr2.tar.gz This will create a directory CSOCppPtr2 and extract the source files into it.|
Once the programme files have been extracted use your favourite editor to view them.
You can compile and run using the following commands;
||Initial Version Released
||Tidied up the makefile and added stdlib.h to CSPCppPtr2.cpp to define EXIT_SUCCESS
Frequently Asked Questions|
Currently there are no FAQ for this "C++ Pointer".
If you have a question, please e-mail it to us. Please ensure that you identify that your question is about our C++ Pointer on Objects, structs and STL Containers.
Currently there are no reported bugs for this "C++ Pointer".
If you suspect a bug in this "C++ Pointer", before contacting us, please check the FAQs and Bug Reports to see if it has already been identified.
If it hasn't, e-mail a description to us. Please ensure you identify that the bug report is about our C++ Pointer on How to handle a member function and variable name collision and include as much detail as possible.
Last modified 01 Jan 10