Avoiding Unnecessary Object Construction (C++)

Avoiding Unnecessary Object Construction
One way to avoid unnecessary object construction is to use a reference instead of an object to store the unnamed temporary object returned by value.

This tip references both Scott Meyers' Effective C++ Item 23 ("Don't try to return a reference when you must return an object.") and Item 20 ("Facilitate the return value optimization.").

Here's the code:


const Rational& operator*(const Rational& lhs, const Rational& rhs)
{
return Rational(lhs.n * rhs.n, lhs.d * rhs.d);
}

Rational a(1, 2);
Rational b(3, 5);
Rational c = a*b; // This incurs an additional object construction.

[const] Rational& c = a*b;
// A better way of doing it. Here we just give the returned temporary object a name 'c'.

Converting Numbers to Strings C++

Converting Numbers to Strings
Instead of writing your own function to convert an integer or float to a string, just use the C++ class stringstream (in the sstream header).

In C++:


#include
#include
#include
using namespace std;

void itoa(int num, string & result)
{
stringstream converter;
converter << result=" converter.str();">

Nested Functions in C++

Nested Functions in C++
Although C++ does not allow nested function declaration, you can simulate it using a structure with a static function:


void outer()
{
static int v1 = 5;
int v2 = 5;

struct thru
{
static void inner()
{
std::cout <<>

The inner() function will not have access to local variables of outer() (the line that is commented out in the code above will cause an error, for example); however, it will be able to access the local static variable v1 in outer().

3D Sample Applications

Version Compatibility: Visual Basic 5 Visual Basic 6

More information: This .zip file contains three applications which illustrate the basics of rendering 3D images using VB's graphics methods. A readme file explaining the underlying logic is included.




This code has been viewed 140303 times.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.

code/3Dprogramming.zip

EPaint - Complete Photo/Image Editor

Version Compatibility: Visual Basic 6

More information: EPaint is a complete photo/image editing program with a lot of great tools and the possibility for making your own filters on the fly.

This code has been viewed 72702 times.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.

source/epaint.zip

A simple chat program

Version Compatibility: Visual Basic 6


More information: This is an application for creating an IRC chat server. Very easy to use. Simply open the Visual Basic Project file (cab_irc.vbp), and run the project. Be sure to click enter at the splash screen.

This code has been viewed 107610 times.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.

Download

Add a Progress Bar and Other Features To Your PPT Presentation

More information: PPT-Pixy is an add-in for MS PowerPoint that aims at dealing with some of the missing features of PowerPoint. The code, a ready-to-use add-in (PPT_Pixy_v110.ppa) and the documentation in PDF format is included in the download. PPT-Pixy offers 4 functionalities: Automatic creation of a slide progress indicator, add page numbers x/X that do not include backup slides, easy to use and precise offset and positioning of objects and easy to use sizing of objects.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.

Download code

Editable ASP Events Calendar

Author: Intelligent Solutions Inc. (Featured Developer)
Category: ASP, HTML, and XML
Type: Applications
Difficulty: Intermediate


Version Compatibility:ASP

More information: This ASP application displays user-added events within a navigatable calendar. Installation and usage instructions are included within a readme.txt file.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.
source/ASP_Calendar.zip

Create a Page-Through ResultSet using Frames

Author: Anonymous
Category: ASP, HTML, and XML
Type: Applications
Difficulty: Intermediate


Version Compatibility:ASP
More information: This application demonstrates how to create a page-through result set on a web page using frames. The records are displayed in the main frame and the status (current page, total pages) are displayed in the bottom frame.

The application uses ASP, client-side VB Script, and DHTML to accomplish this, so it is a good demonstration of all these technologies.
IE 4.0 or above is required.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.
code/ASPFrameDB1.zip

Complete Visual Basic Web Site

Author: Anonymous
Category: ASP, HTML, and XML
Type: Applications
Difficulty: Intermediate


Version Compatibility:ASP

More information: This is a complete web site, devoted to Visual Basic, ready to run out of the box. It includes sample code, articles, and links sections, and a feedback and search feature.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.
code/ASPHercules.zip

An XML-COM+ replacement for the ASP Session object


Author:Sebastian Odin Smith
Category:ASP, HTML, and XML
Type:Classes
Difficulty:Advanced

Version Compatibility:Visual Basic 5 Visual Basic 6


More information: In order to maintain client session state within a web application that is hosted in a server farm environment a method of storing state must be designed and implemented. Within a Microsoft environment, session state can be stored in a number of ways, each with it's own pros and cons.

The use of ASP Session variables is the easiest to use, but the least scalable. Querystring and form information are the most scalable, but they can be difficult to use and are totally unsecured. In order to find a compromise between the available session persistent methods, the xmlSession component was created.

This component will provide the web developer with method to easily maintain session state without the scalability problems associated with traditional session variables. All "session" information is stored in a centrally located data store (file or database) that can be accessed by any server in the farm.


Notes: Windows 2000 and version 3.0 or higher of the Microsoft's XML object model is required. Complete documentation is included. There is also a test vb project and and project group (.vbg) included in the test directory. Running the test project and stepping through the code will give you an idea of what the module does.

Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.
code/xmlSession.zip

ADO Access 2000 VB Code Generator

Author: Carlos Vara
Category: Database
Type: Applications
Difficulty: Intermediate
Version Compatibility: Visual Basic 5 Visual Basic 6


More information: this utilitie generate code that you can use inside your apps to create an Access 2000 Database With ADO. You select the database and it generates the code to create a database with the same name and structure.
This code has been viewed 166390 times.
Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.

source/ADOCodeGen.zip

ADO Utilities Class Version 2.0

Author:Brian Gillham (Featured Developer)
Category:
Database
Type:
Classes
Difficulty:
Intermediate
Version Compatibility:Visual Basic 6


More information: This class encapsulates many routine tasks in ADO programming, which reduces the tedium and increases the reusability of your code. For example, if you pass it a valid connection string, it will automatically set up a connection, if you pass it a valid SQL string, it will return a recordset, and so on. It includes a function that returns a shaped (parent-child) recordset, without you having to deal with writing the complex SQL that is required for shaped recordsets.
Version 2.0, added 06/06/02, incorporates the SMARTSql class on this site and also adds improvements such as automatic selection of cursors. There is a sample project; you may have to modify the connection string that is included as a constant to get it the sample to work.
This code has been viewed 153273 times.
Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.
source/ADOUtils.zip