C++ const
From http://www.cs.duke.edu/csed/cplus/const/
1. Pass by Const Reference to combine efficiency and safety.
void foo(int const ¶m)
The pass-by-reference (indicated by the & in the parameter) means that no copy is made in passing an argument. The const modifier means that the parameter cannot be modified within the body of foo. The reference is for efficiency and the const is for safety.
Note:
The C++ standard requires a const-modified reference parameter for literal/temporary arguments; a reference parameter without the const modifier won't support literal/constant arguments
2. Const Member function
char operator[ ]( int k ) const; // for const strings
char & operator[ ]( int k ); // for non-const strings
The string indexing functions would then be rewritten as:
char operator[ ](const string & self, int k ); // for const strings
char & operator[ ](string & self, int k ); // for non-const strings
string s="abc";
print(s[1]); //const string
s[1]='d'; //non-const string
3. Mutable data
class List
{
private:
mutable Node *myCurrentPtr;
public:
void next() const;
};
void List::next() const {
myCurrentPtr = myCurrentPtr->next;
}
A mutable data member can be modified by a const function.
Programming with const can be painful. It's easy to miss the appearance of const in compiler error messages --- be sure that you look for it when you get a "member function foo not implemented" error. You'll also get the function signature/prototype --- look for const to see if you put a const in the header file, for example, but forgot to add the const when defining the function.
Common Linux command
- use "grep" and "find" to search patterns in all specified files
find . -type f -name *.cpp xargs grep "patterns"
- use cvs diff to log the difference between different version
cvs diff -u4 -r1.7 -r1.8 Makefile
- use ps to check process
ps -elf
C++ reference and initialization
void test(string& s)
{
}
I can call this function with a string var:
string s("abc");
test(s);
But I cannot do this:
test(string("abc"));
But MS C/C++ compiler (Visual C++ Toolkit) compiles the above code with no problem.
I thought it was acceptable - but it is not.
However, if I declare the parameter to be const, like this:
void (const string& s)
Then I can do test(string("abc")) and EVEN test("abc")!
The reason:
When you declare a function like this...
void test(string& s);
You are telling the compiler "the test function is going to modify the parameters". That is to say, s is an output (or update) parameter.
When you call the function like this...
test(string("abc"));
...you are creating a temporary, anonymous string object, and passing it in to test. The compiler *KNOWS* (because you told it) that the string parameter is an output parameter. The compiler also knows that a temporary, anonymous used as an output parameter is 99% likely to be a mistake
-- why would you intentionally want to throw away the results of the routine? So likely is it to be a mistake, that it is actually disallowed by the ISO 14882 C++ standard -- it's a C++ programming error.
(Also, because of helpful conversions, you may get temporaries when you didn't expect them. That is the compelling reason that this situation is a disallowed. Your example is "obvious it is a temporary; it's explicit" ... but that's because it is a simple and straightforward example.)
When you declare the function...
void test(string const& s);
...you are telling the compiler that the parameter is an input only parameter, and hence temporaries are okay.
If you really don't like the parameter to be const, you could also do this...
void test(string s);
...and then the compiler *KNOWS* that the pass-by-value parameter is not an output parameter (so temporaries are okay) *AND* you can modify it within the routine.
ICWS2005 presentation slides
The revised slides are also available at
http://rommel.cs.binghamton.edu/~pliu/revised-ICWS2005-presentation.ppt
source navigator
Source-Navigator is a source code analysis tool. With it, you can edit your source code, display relationships between classes and functions and members, and display call trees.
http://sourceforge.net/projects/sourcenav
C++ class circular reference
Here is the thorough example:
- class A refers to class C
- class C refers to class A
The tricks are the forward reference and the avoidance of double including.
Then, we are free to go.
------------------------A.cpp---------------------
#include "C.h" //important, it is required to get definition of class C
#include "A.h"
void A::SomeMethod()
{
m_pC = new C();
m_pC-SomeMethod();
}
------------------------End A.cpp---------------------
--------------------------- A.h ------------------
#ifndef A_H //to avoid double including
#define A_H
/* Class A definition */
class C; //forward reference
class A
{
public:
A(){};
~A(){};
void SomeMethod();
private:
C* m_pC;
};
#endif
---------------------------End A.h-------------------------
--------------------------- C.h ------------------
#ifndef C_H //to avoid double including
#define C_H
/* Class C definition */
#include "A.h"
class C
{
public:
C(){};
~C(){};
void SomeMethod(){};
private:
A* m_pA;
};
#endif
---------------------------End C.h-------------------------
----------------------------main.cpp----------------------
#include "C.h"
#include
int main()
{
C c;
c.SomeMethod();
}
---------------------------End main.cpp-----------------
Compile: g++ -c A.cpp; g++ -c main.cpp; g++ -o m main.o A.o
[CodeProject] Recommendation: "CFileVersionInfo - Getting the file version information"
Hello from The Code Project!
This email was created by pu liu <pliu1@binghamton.edu> who thought you would be interested in the following article:
"CFileVersionInfo - Getting the file version information"
Class for getting file version information
http://www.codeproject.com/file/VersionInfo.asp
CodeProject also contains hundreds of articles, free source code samples and applications to help in your day to day programming. Why not give it a try!
Additional comments:
For version info
Best regards,
The CodeProject Team
------------
The Code Project - designed for developers
http://www.CodeProject.com
Win32 Resource file
Resources can be composed of a wide range of elements, including interface elements that provide information to the user (for example a bitmap, icon, or cursor); custom resources that contain data an application needs; version resources that are used by setup APIs; and menu and dialog box resources.
You can add new resources to your project and modify those resources using the appropriate resource editor. Most Visual C++ wizards will automatically generate an .rc file for your project.
Note Because managed projects (Visual Basic, Visual C#, and Managed Extensions for C++) do not use resource script files, you must open your resources from Solution Explorer. You can use the Image editor and the Binary editor to work with resource files in managed projects. Any managed resources you want to edit must be linked resources. The Visual Studio resource editors do not support editing embedded resources.
For information on adding resources to managed projects, please see Resources in Applications in the .NET Framework Developer's Guide. For information on manually adding resource files to managed projects, accessing resources, displaying static resources, and assigning resources strings to properties, see Walkthrough: Localizing Windows Forms and Walkthrough: Localizing Web Forms.
In This Section
Describes resource files and how they are used in Win32-based applications. Also provides links to topics that describe how to use resource files.
Describes symbols and provides information on using the Resource Symbols dialog box to manage symbols in your projects.
Describes the resource editors provided in Visual Studio, the types of resources you can modify with each editor, and provides links to detailed information on using each editor.
Related Sections
A section of the Platform SDK that provides general information on resources you can add to Win32-based applications.
Provides links into the Visual C++ documentation.
Introducing Visual Studio .NET
Describes the complete set of development tools that all use the same integrated development environment (IDE), allowing them to share tools and facilitates in the creation of mixed-language solutions.
Provides links to information on using the documentation set, contacting product support, and employing accessibility features.
How to build VC program
You can build projects in Visual C++ in one of two ways:
- Using Visual Studio .NET.
- Using the command line.
In This Section
Building C++ Projects in Visual Studio
Discusses using the Visual Studio integrated development environment (IDE) and property pages to build your project.
Discusses command-line tools for programmers who prefer to build their applications from the command prompt.
Provides links to an overview of building programs in C++, compiler and linker options, and additional build tools.
Related Sections
Provides links to different areas of the Visual Studio and Visual C++ documentation set.
Provides links to topics describing the C and C++ language references, the libraries provided with Visual C++, the Visual C++ Extensibility Object Model, and the Microsoft Macro Assembler (MASM).
Provides links to sample code showing the capabilities of Visual C++ and the libraries and technologies it supports.
VC++ Linking Options
LINK is a 32-bit tool that links Common Object File Format (COFF) object files and libraries to create a 32-bit executable (.exe) file or dynamic-link library (DLL).
The table below is a comprehensive list of options for LINK.exe. This section also includes information on:
Linker options specified on the command line are not case sensitive: /base and /BASE mean the same thing.
You can specify some linker options via the comment pragma.
VC++ Compiler (cl.exe) Options
The following is a comprehensive categorical list of compiler options. For an alphabetical list, see Compiler Options Listed Alphabetically.
XML extensibility by Dave, BEA
Web site
Two papers in xml.com
XML extensibility
http://www.xml.com/pub/a/2004/10/27/extend.html
Web service pitfalls
http://webservices.xml.com/pub/a/ws/2002/02/06/webservices.html
blog
http://www.pacificspirit.com/blog/
writings
http://www.pacificspirit.com/Authoring/