Suppose you have a class declare in a namespace, like this:
#pragma once
namespace Test
{
class foo
{
public:
foo(void);
~foo(void);
};
};
#include "foo.h"
namespace Test
{
foo::foo(void)
{
}
foo::~foo(void)
{
}
};
And you want to use that foo class in a another class, with a forward declaration:
#pragma once
class foo;
class bar
{
foo * m_foo;
public:
bar(void);
~bar(void);
};
#include "bar.h"
#include "foo.h"
using namespace Test;
bar::bar(void)
{
m_foo = new foo;
}
bar::~bar(void)
{
}
Most likely you’ll get some errors, like this (issues by VS2008 compiler)
error C2872: ‘foo’ : ambiguous symbol
error C2512: ‘foo’ : no appropriate default constructor available
The problem is that for the compiler it is ambiguous whether that foo and Test::foo are the same or not. To remove the ambiguity you should specify that the forward declared foo type is the same with the one from the namespace Test. The best way to do that is like this:
namespace Test
{
class foo;
};
class bar
{
Test::foo * m_foo;
public:
bar(void);
~bar(void);
};
The only thing that I should mention is that approach is not allowed for the types from namespace std, as Herb Sutter explains here.
Hits for this post: 8159 .
Suppose you need a ListBox that has to display items with the text spread across multiple lines. With the default implementation that would look like this:

The CR LF characters are displayed as squares and the item has only one line of text. To make the list box items have variable height depending on the number of lines of the text you need an owner draw ListBox.
That requires two this:
- set the DrawMode property to OwnerDrawVariable
- handle the MeasureItem and DrawItem event; the first one is fired when the size of the item (height and width) must be computed, and the second when the item is to be displayed
listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_MeasureItem);
A simple implementation for these handlers is:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(
(string)listBox1.Items[e.Index],
e.Font,
new SolidBrush(e.ForeColor),
e.Bounds);
}
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 13 * GetLinesNumber((string)listBox1.Items[e.Index]);
}
private int GetLinesNumber(string text)
{
int count = 1;
int pos = 0;
while ((pos = text.IndexOf("\r\n", pos)) != -1) { count++; pos += 2; }
return count;
}
With those changes made the items should now look like this:


And that was fast and easy!
Hits for this post: 13881 .
STL’s std::string doesn’t have methods for transforming the string to upper or lower case or for trimming (as other string implementations, such as CString, have). I’ve noticed that many people don’t know how to implement such functions, so I’ll show you here how to do that.
class stringutils
{
public:
static void make_upper(std::string& text)
{
std::transform(text.begin(), text.end(), text.begin(), toupper);
}
static void make_lower(std::string& text)
{
std::transform(text.begin(), text.end(), text.begin(), tolower);
}
static void trim_left(std::string& text)
{
if(!text.empty())
{
std::string::iterator pos = text.begin();
while(pos != text.end() && isspace(*pos)) pos++;
text.erase(text.begin(), pos);
}
}
static void trim_left(std::string& text, const char* toremove)
{
if(!text.empty())
{
std::string::size_type pos = text.find_first_not_of(toremove);
if(std::string::npos != pos) text.erase(0, pos);
else text.clear();
}
}
static void trim_right(std::string& text)
{
if(!text.empty())
{
std::string::iterator pos = text.end() - 1;
while(pos != text.begin() && isspace(*pos)) pos--;
text.erase(pos+1, text.end());
}
}
static void trim_right(std::string& text, const char* toremove)
{
if(!text.empty())
{
std::string::size_type pos = text.find_last_not_of(toremove);
if(std::string::npos != pos) text.erase(pos + 1);
else text.clear();
}
}
};
You can use it like this:
std::string text = " \r\n sample text \r\n ";
stringutils::make_upper(text);
std::cout << text << std::endl;
stringutils::trim_left(text);
std::cout << text << std::endl;
stringutils::trim_right(" \r\n");
std::cout << text << std::endl;
Hits for this post: 7895 .