数据加载中……

【转】关于C++虚函数

     摘要: C++ 虚函数   阅读全文

posted @ 2010-06-17 11:52 Stone xin 阅读(233) | 评论 (0)编辑 收藏

【wxWidgets】从外部调用wxDLL

     摘要: wxWidgets dll外部调用  阅读全文

posted @ 2010-06-07 11:11 Stone xin 阅读(270) | 评论 (0)编辑 收藏

进一步分析vector的删除

项目开发中用到了vector,自己定义了一个数据类型 myType,用vector来保存。
在删除的时候出现了个问题。


#include <vector>
#include <algorithm>
class MyClass
{
public:
    MyClass(int v){ m_value=v;}
    virtual ~MyClass(){}
    MyClass  operator=(const MyClass& otherData); 
    bool  operator==(const MyClass& otherData); 

    int m_value;
}

MyClass::operator =(const MyClass &otherData)
{
    return *this;
}

bool MyClass::operator ==(const MyClass &otherData)
{
    if (otherData.m_value==this->m_value)
    {
        return true;
    }else
    {
        return false;
    }
}


int main()
{
    std::vector<MyClass> myVector;
    MyClass v1(1);
    MyClass v2(2);
    MyClass v3(3);
    myVector.push_back(v1);
    myVector.push_back(v2);
    myVector.push_back(v3);

    for( std::vector<MyClass>::iterator result=myVector.begin();
        result!=myVector.end();
        ++result) 
    {
        cout << "Element " << result->m_value << endl;
    }

    MyClass v(2);
    std::vector<MyClass>::iterator it = std::find(myVector.begin(),myVector.end(),v);
    if (it!=myVector.end())
    {
        myVector.erase(it);
    }
    for( std::vector<MyClass>::iterator result=myVector.begin();
        result!=myVector.end();
        ++result) 
    {
        cout << "After erase MyClass v(2), Element left " << result->m_value << endl;
    }
    return 0;
}




猜猜看输出结果会是什么?
如果你想当然的认为是
Element 1
Element 2
Element 3
After erase MyClass v(2), Element left 1
After erase MyClass v(2), Element left 3
那就错了!
Element 1
Element 2
Element 3
After erase MyClass v(2), Element left 1
After erase MyClass v(2), Element left 2
这个才是结果。
这当中因为重载了赋值运算符,vector在删除一个元素的时候,我猜测是把后一个元素的值,赋给了删除的。如果赋值操作符中没有处理得当,那么就会出现令人摸不着头脑的问题。

posted @ 2009-12-10 17:41 Stone xin 阅读(1473) | 评论 (0)编辑 收藏

[wxWidgets]Embedding PNG images into executables(将png嵌入到程序中)

首先需要用工具将png图片转成.cpp或者.h文件
工具:bin2c
源代码:

// bin2c.c
//
// convert a binary file into a C source vector
//
// THE "BEER-WARE LICENSE" (Revision 3.1415):
// sandro AT sigala DOT it wrote this file. As long as you retain this notice you can do
// whatever you want with this stuff.  If we meet some day, and you think this stuff is
// worth it, you can buy me a beer in return.  Sandro Sigala
//
// syntax:  bin2c [-c] [-z] <input_file> <output_file>
//
//          -c    add the "const" keyword to definition
//          -z    terminate the array with a zero (useful for embedded C strings)
//
// examples:
//     bin2c -c myimage.png myimage_png.cpp
//     bin2c -z sometext.txt sometext_txt.cpp

#include 
<ctype.h>
#include 
<stdio.h>
#include 
<stdlib.h>
#include 
<string.h>

#ifndef PATH_MAX
#define PATH_MAX 1024
#endif

int useconst = 0;
int zeroterminated = 0;

int myfgetc(FILE *f)
{
    
int c = fgetc(f);
    
if (c == EOF && zeroterminated)
    
{
        zeroterminated 
= 0;
        
return 0;
    }

    
return c;
}


void process(const char *ifname, const char *ofname)
{
    FILE 
*ifile, *ofile;
    errno_t err 
= fopen_s(&ifile,ifname,"rb");
    
if (ifile == NULL)
    
{
        fprintf(stderr, 
"cannot open %s for reading\n", ifname);
        exit(
1);
    }

    fopen_s(
&ofile,ofname, "wb");
    
if (ofile == NULL)
    
{
        fprintf(stderr, 
"cannot open %s for writing\n", ofname);
        exit(
1);
    }

    
char buf[PATH_MAX], *p;
    
const char *cp;
    
if ((cp = strrchr(ifname, '/')) != NULL)
    
{
        
++cp;
    }
 else {
        
if ((cp = strrchr(ifname, '\\')) != NULL)
            
++cp;
        
else
            cp 
= ifname;
    }

    strcpy_s(buf, cp);
    
for (p = buf; *!= '\0'++p)
    
{
        
if (!isalnum(*p))
            
*= '_';
    }

    fprintf(ofile, 
"static %sunsigned char %s[] = {\n", useconst ? "const " : "", buf);
    
int c, col = 1;
    
while ((c = myfgetc(ifile)) != EOF)
    
{
        
if (col >= 78 - 6)
        
{
            fputc(
'\n', ofile);
            col 
= 1;
        }

        fprintf(ofile, 
"0x%.2x, ", c);
        col 
+= 6;
    }

    fprintf(ofile, 
"\n};\n");

    fclose(ifile);
    fclose(ofile);
}


void usage(void)
{
    fprintf(stderr, 
"usage: bin2c [-cz] <input_file> <output_file>\n");
    exit(
1);
}


int main(int argc, char **argv)
{
    
while (argc > 3)
    
{
        
if (!strcmp(argv[1], "-c"))
        
{
            useconst 
= 1;
            
--argc;
            
++argv;
        }
 else if (!strcmp(argv[1], "-z"))
        
{
            zeroterminated 
= 1;
            
--argc;
            
++argv;
        }
 else {
            usage();
        }

    }

    
if (argc != 3)
    
{
        usage();
    }

    process(argv[
1], argv[2]);
    
return 0;
}

在官方论坛中的做了些修改。去掉了一些警告信息。

2、在你的程序中包含转换好的cpp或者h
 #include <myimage_png.cpp>

wxMemoryInputStream istream(myimage_png, sizeof myimage_png);
wxImage myimage_img(istream, wxBITMAP_TYPE_PNG);

或者可以定义个宏

#define wxGetBitmapFromMemory(name) _wxGetBitmapFromMemory(name ## _png, sizeof(name ## _png))
inline wxBitmap _wxGetBitmapFromMemory(const unsigned char *data, int length) {
wxMemoryInputStream is(data, length);
return wxBitmap(wxImage(is, wxBITMAP_TYPE_ANY, -1), -1);
}
注意:要确保png的句柄初始化了。wxImage::AddHandler(new wxPNGHandler);

posted @ 2009-12-04 11:07 Stone xin 阅读(481) | 评论 (0)编辑 收藏

Ticpp demo程序

将网上的TinyXML++的demo程序用vs2005建了个sample_ticpp工程,供大家学习参考。

posted @ 2009-12-01 15:18 Stone xin 阅读(297) | 评论 (0)编辑 收藏

Doxygen使用

     摘要: Doxygen使用  阅读全文

posted @ 2009-10-10 17:49 Stone xin 阅读(251) | 评论 (0)编辑 收藏

c++参考手册

经常用到的东西,但一时总是找不到在哪里下载,所以把地址放到这里,备忘
http://www.cppreference.com/wiki/about/faq

最新的手册地址是:c++手册

posted @ 2009-10-06 12:40 Stone xin 阅读(658) | 评论 (0)编辑 收藏

仅列出标题
共4页: 1 2 3 4